SEQ  Recursive Sequence
Sequence (a_{i}) of natural numbers is defined as follows:
a_{i} = b_{i} (for i <= k)
a_{i} = c_{1}a_{i1} + c_{2}a_{i2} + ... + c_{k}a_{ik} (for i > k)
where b_{j} and c_{j} are given natural numbers for 1<=j<=k. Your task is to compute a_{n} for given n and output it modulo 10^{9}.
Input
On the first row there is the number C of test cases (equal to about 1000).
Each test contains four lines:
k  number of elements of (c) and (b) (1 <= k <= 10)
b_{1},...,b_{k}  k natural numbers where 0 <= b_{j} <= 10^{9} separated by spaces
c_{1},...,c_{k}  k natural numbers where 0 <= c_{j} <= 10^{9} separated by spaces
n  natural number (1 <= n <= 10^{9})
Output
Exactly C lines, one for each test case: a_{n} modulo 10^{9}
Example
Input: 3 3 5 8 2 32 54 6 2 3 1 2 3 4 5 6 6 3 24 354 6 56 57 465 98765432 Output: 8 714 257599514
hide comments
tanish_29:
20230211 05:45:59
If you're coding in Java and are struggling with mod, I used long instead of int and then used modulus and it worked for me. Last edit: 20230211 05:47:44 

coutaditya:
20211223 15:42:33
Accepted in one go :) 

terminator0309:
20210719 11:03:18
here MOD is 1e9 not 1e9+7 

blueflare99:
20210608 11:07:10
Guys don't fear about fixing %MOD while doing matrix multiplication. It will remain like this only:


jaydip001:
20210516 12:22:28
At last solved the excellent question 

terminator2001:
20210410 15:15:28
bc


maheshwari7778:
20210320 09:24:20
Amazing blog


agga_daku:
20210131 09:59:44
i am getting wrong answer can anybody help? 

ashigup11:
20210116 09:06:31
AC in one go


coding_sourabh:
20210114 08:47:20
I really wanted to write it atleast once :P , AC in one Go

Added by:  PaweÅ‚ Dobrzycki 
Date:  20050429 
Time limit:  0.5s3s 
Source limit:  8196B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All 
Resource:  IV Podlasian Contest in Team Programming 