National Olympiad in Informatics, China, 2013
TingTing is a girl that loves matrices. One day, she wants to use a
computer to generate a giant
row by
column matrix (you don't
have to worry about how she'll store it). Her generated matrix will
satisfy a mystical property: if we use
to represent the
cell in the
-th row and
-th column, then
will
satisfy the following system of equations:
![\displaystyle \begin{cases}
F[1][1] = 1 \\
F[i][j] = a \times F[i][j-1]+b && j \ne 1 \\
F[i][1] = c \times F[i-1][m]+d && i \ne 1
\end{cases}](//static.dmoj.ca/mathoid/139428e95d35d50ab3d17054d1898ca05ddf91b8/svg)
where
,
,
, and
are given constants.
TingTing would like to know the value of
and she would
like you to help her. Since the final value may be very large, you are
only required to output it modulo
.
Input Specification
The input will contain the six integers
,
,
,
,
, and
.
Output Specification
Output a single integer, the value of
modulo
.
Sample Input
Copy
3 4 1 3 2 6
Sample Output
Copy
85
Explanation
The matrix in the example is:

Constraints
Test Case |
Constraints |
1 |
;  |
2 |
;  |
3 |
;  |
4 |
5 |
; ;  |
6 |
; ;  |
7 |
 |
8 |
9 |
10 |
11 |
; ;  |
12 |
; ;  |
13 |
;  |
14 |
15 |
;  |
16 |
17 |
; ;  |
18 |
; ;  |
19 |
;  |
20 |
Problem translated to English by Alex.
Comments
Since the original data were weak, two additional test cases were added and weighted identically to the others, and all submissions were rejudged.