#P1349. 广义斐波那契数列

广义斐波那契数列

Description

A generalized Fibonacci sequence is a sequence of the form an=p×an1+q×an2a_n=p\times a_{n-1}+q\times a_{n-2}.

Given the two coefficients pp and qq, the first two terms a1a_1 and a2a_2, and two integers nn and mm, compute the nn-th term ana_n modulo mm.

Input Format

The input contains one line with six integers: p,q,a1,a2,n,mp,q,a_1,a_2,n,m.

Output Format

Output a single integer on one line: the answer.

1 1 1 1 10 7

6

Hint

The 1010 -th term is 5555, and 55mod7=655 \bmod 7 = 6.

Constraints
For 100%100\% of the testdata, p,q,a1,a2[0,2311]p,q,a_1,a_2 \in [0,2^{31}-1], 1n,m23111\le n,m \le 2^{31}-1.


upd 2023.8.23\text{upd 2023.8.23}: Added a new set of hack testdata.

Translated by ChatGPT 5