QOJ.ac

QOJ

时间限制: 1 s 内存限制: 256 MB 总分: 100

#6780. Matrix Game

统计

Tingting is a child who loves matrices. One day, she wants to use a computer to generate a huge $n \times m$ matrix (you don't need to worry about how she stores it). The matrix she generates satisfies a magical property: if we use $F[i][j]$ to represent the element in the $i$-th row and $j$-th column of the matrix, then $F[i][j]$ satisfies the following recurrence relations:

$$ \begin{cases} F[1][1] = 1 \\ F[i][j] = a \times F[i][j-1] + b & j \neq 1 \\ F[i][1] = c \times F[i-1][m] + d & i \neq 1 \end{cases} $$

In the recurrence relations, $a, b, c, d$ are given constants.

Now, Tingting wants to know the value of $F[n][m]$. Please help her. Since the final result may be very large, you only need to output the remainder of $F[n][m]$ divided by $1,000,000,007$.

Input

The input contains one line with six integers $n, m, a, b, c, d$. The meanings are as described above.

Output

Output one integer, representing the remainder of $F[n][m]$ divided by $1,000,000,007$.

Examples

Input 1

3 4 1 3 2 6

Output 1

85

Note 1

The matrix in the example is: $$ \begin{pmatrix} 1 & 4 & 7 & 10 \\ 26 & 29 & 32 & 35 \\ 76 & 79 & 82 & 85 \end{pmatrix} $$

Input 2

See Download Files: matrix.in and matrix.out.

Constraints

Test Case ID Data Range
1 $1 \le n, m \le 10$; $1 \le a, b, c, d \le 1000$
2 $1 \le n, m \le 100$; $1 \le a, b, c, d \le 1000$
3 $1 \le n, m \le 10^3$; $1 \le a, b, c, d \le 10^9$
4 $1 \le n, m \le 10^3$; $1 \le a, b, c, d \le 10^9$
5 $1 \le n, m \le 10^9$; $1 \le a = c \le 10^9$; $1 \le b = d \le 10^9$
6 $1 \le n, m \le 10^9$; $a = c = 1$; $1 \le b, d \le 10^9$
7 $1 \le n, m, a, b, c, d \le 10^9$
8 $1 \le n, m, a, b, c, d \le 10^9$
9 $1 \le n, m, a, b, c, d \le 10^9$
10 $1 \le n, m, a, b, c, d \le 10^9$
11 $1 \le n, m \le 10^{1,000}$; $a = c = 1$; $1 \le b, d \le 10^9$
12 $1 \le n, m \le 10^{1,000}$; $1 \le a = c \le 10^9$; $1 \le b = d \le 10^9$
13 $1 \le n, m \le 10^{1,000}$; $1 \le a, b, c, d \le 10^9$
14 $1 \le n, m \le 10^{1,000}$; $1 \le a, b, c, d \le 10^9$
15 $1 \le n, m \le 10^{20,000}$; $1 \le a, b, c, d \le 10^9$
16 $1 \le n, m \le 10^{20,000}$; $1 \le a, b, c, d \le 10^9$
17 $1 \le n, m \le 10^{1,000,000}$; $a = c = 1$; $1 \le b, d \le 10^9$
18 $1 \le n, m \le 10^{1,000,000}$; $1 \le a = c \le 10^9$; $1 \le b = d \le 10^9$
19 $1 \le n, m \le 10^{1,000,000}$; $1 \le a, b, c, d \le 10^9$
20 $1 \le n, m \le 10^{1,000,000}$; $1 \le a, b, c, d \le 10^9$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.