Tricky Tribonacci
By Kay Akashi
Time: 1000 ms
Memory: 256000 kB
The code judging system is only available during contests. Check out the
github repo for test cases and solutions.
Problem Statement
Sequence a meets the following recursion: an+3=pan+2+qan+1+ran. Provided that a1=x,a2=y, and a3=z, your task is to output the value of ak. However, because the answer can become astronomically large, you are required to output ak mod 109+7. In other word, output the remainder of ak divided by 109+7.
Constraints
1≤p,q,r,x,y,z≤105, 1≤k≤1012.
Input
The first line of input contains three integers, p, q, and r.
The second line of input contains three integers, x, y, and z.
The third line of input contains one integer, k.
Output
Output the answer.
Examples
Input
Output
Explanation
In this case a1=1,a2=4,a3=9, so a4=3a3+2a2+7a1=42.
Input
13238 91313 41471
12374 58241 31231
831487118462
Output