A矩阵：

B矩阵：

#### C++代码

```#include <cstdio>
#include <map>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

typedef unsigned long long ULL;

const int MAXN = 7;
const ULL MOD = 1000000007;
int N = 5;

struct Mat {
ULL a[MAXN][MAXN];
Mat() { memset(a, 0, sizeof(a)); }
Mat multiMod(const Mat & y, const ULL mod) {
Mat res;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
for(int k = 0; k < N; k++)
res.a[i][j] = (res.a[i][j] + (a[i][k] * y.a[k][j]) % mod) % mod;
return res;
}
Mat powerMod(ULL k, ULL mod) {
Mat res, base = *this;
for(int i = 0; i < MAXN; i++)
res.a[i][i] = 1;
while(k > 0) {
if(k & 1)
res = res.multiMod(base, mod);
base = base.multiMod(base, mod);
k >>= 1;
}
return res;
}
};

int main() {
ULL n, a0, ax, ay, b0, bx, by;
while(~scanf("%I64u", &n)) {
scanf("%I64u%I64u%I64u", &a0, &ax, &ay);
scanf("%I64u%I64u%I64u", &b0, &bx, &by);
if(n == 0) {
printf("0\n");
continue;
}
if(n == 1) {
printf("%I64u\n", (a0*b0) % MOD);
continue;
}
Mat A, B;
A.a[0][0] = (a0*b0) % MOD;
A.a[1][0] = a0 % MOD;
A.a[2][0] = b0 % MOD;
A.a[3][0] = 1;
A.a[4][0] = 0;

B.a[0][0] = (ax*bx)%MOD;
B.a[0][1] = (ax*by)%MOD;
B.a[0][2] = (ay*bx)%MOD;
B.a[0][3] = (ay*by)%MOD;
B.a[1][1] = ax % MOD;
B.a[1][3] = ay % MOD;
B.a[2][2] = bx % MOD;
B.a[2][3] = by % MOD;
B.a[3][3] = 1LL;
B.a[4][0] = B.a[4][4] = 1LL;

B = B.powerMod(n-1, MOD);
B = B.multiMod(A, MOD);
printf("%I64u\n", (B.a[0][0] + B.a[4][0]) % MOD);
}
return 0;
}
```