//#define int long long constint maxn = 2e5 + 5; constint maxm = maxn << 1; int n, m; constint mod = 1e9 + 7; structMatrix { int a[2][2]; Matrix(void) { memset(a, 0, sizeof(a)); } Matrix operator * (const Matrix &z) const { Matrix res; for(int i = 0; i < 2; ++ i) { for(int j = 0; j < 2; ++ j) { for(int k = 0; k < 2; ++ k) { res.a[i][j] = (res.a[i][j] + 1ll * a[i][k] * z.a[k][j] % mod) % mod; } } } return res; } }F, tmp1, tmp2; string sn, sm; int A, B, C, D;
Matrix ksm(Matrix x,int mi){ Matrix res; res.a[0][0] = res.a[1][1] = 1; while(mi) { if(mi & 1) res = res * x; mi >>= 1; x = x * x; } return res; }
signedmain(){ // freopen("S.in", "r", stdin); // freopen("S.out", "w", stdout); int i, j; cin >> sn >> sm; r1(A, B, C, D);// A <= C int mod1 = ( (A == 1) ? mod : mod - 1 ); for(i = 0; i < sm.size(); ++ i) m = ( 10ll * m + (sm[i] ^ 48) ) % mod1;
// printf("%d %d\n", n, m);
F.a[0][0] = F.a[0][1] = 1; tmp1.a[0][0] = 1, tmp1.a[1][0] = 0; tmp1.a[0][1] = B, tmp1.a[1][1] = A; tmp2.a[0][0] = 1, tmp2.a[1][0] = 0; tmp2.a[0][1] = D, tmp2.a[1][1] = C; tmp1 = ksm(tmp1, m < 1 ? m + mod1 - 1 : m - 1); tmp2 = tmp1 * tmp2; mod1 = (tmp2.a[1][1] == 1 ? mod : mod - 1); for(i = 0; i < sn.size(); ++ i) n = ( 10ll * n + (sn[i] ^ 48) ) % mod1; F = F * ksm(tmp2, n < 1 ? n + mod1 - 1 : n - 1) * tmp1; printf("%d\n", F.a[0][1]); return0; } /* 34578734657863487 38465873465876348 1 23734637 3892734 3849 467549493 */