#P50769. 「NOI2013」矩阵游戏

「NOI2013」矩阵游戏

题目描述

婷婷是个喜欢矩阵的小朋友,有一天她想用电脑生成一个巨大的 nnmm 列的矩阵(你不用担心她如何存储)。她生成的这个矩阵满足一个神奇的性质:若用 F[i][j]F[i][j] 来表示矩阵中第 ii 行第 jj 列的元素,则 F[i][j]F[i][j] 满足下面的递推式:

{F[1][1]=1F[i][j]=aF[i][j1]+bj1F[i][1]=cF[i1][m]+di1\begin{cases} F[1][1] = 1 \\ F[i][j] = a * F[i][j-1]+b & j \neq 1 \\ F[i][1] = c * F[i-1][m]+d & i \neq 1 \end{cases}

递推式中 aabbccdd 都是给定的常数。

现在婷婷想知道 F[n][m]F[n][m] 的值是多少,请你帮助她。由于最终结果可能很大,你只需要输出 F[n][m]F[n][m] 除以 1 000 000 0071\ 000\ 000\ 007 的余数。

输入格式

一行有六个整数 nnmmaabbccdd。意义如题所述。

输出格式

包含一个整数,表示 F[n][m]F[n][m] 除以 1 000 000 0071\ 000\ 000\ 007 的余数。

样例

样例输入 1

3 4 1 3 2 6

样例输出 1

85

样例解释 1

样例中的矩阵为:(147102629323576798285)\begin{pmatrix} 1& 4& 7& 10\\ 26& 29& 32& 35\\ 76& 79& 82& 85\end{pmatrix}

样例输入输出 2

见附加文件中的 matrix.inmatrix.ans.

数据范围与提示

测试点编号 数据范围
1 1n,m101 \le n,m \le 101a,b,c,d10001 \le a,b,c,d \le 1000
2 1n,m1001 \le n,m \le 1001a,b,c,d10001 \le a,b,c,d \le 1000
3 1n,m1031 \le n,m \le 10^31a,b,c,d1091 \le a,b,c,d \le 10^9
4
5 1n,m1091 \le n,m \le 10^91a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
6 1n,m1091 \le n,m \le 10^9a=c=1a = c = 11b,d1091 \le b,d \le 10^9
7 1n,m,a,b,c,d1091 \le n,m,a,b,c,d \le 10^9
8
9
10
11 1n,m1010001 \le n,m \le 10^{1000}a=c=1a = c = 11b,d1091 \le b,d \le 10^9
12 1n,m1010001 \le n,m \le 10^{1000}1a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
13 1n,m1010001 \le n,m \le 10^{1000}1a,b,c,d1091 \le a,b,c,d \le 10^9
14
15 1n,m10200001 \le n,m \le 10^{20000}1a,b,c,d1091 \le a,b,c,d \le 10^9
16
17 1n,m101,000,0001 \le n,m \le 10^{1,000,000}a=c=1a = c = 11b,d1091 \le b,d \le 10^9
18 1n,m101,000,0001 \le n,m \le 10^{1,000,000}1a=c1091 \le a = c \le 10^91b=d1091 \le b = d \le 10^9
19 1n,m101,000,0001 \le n,m \le 10^{1,000,000}1a,b,c,d1091 \le a,b,c,d \le 10^9
20