1 /**************************************************************
2 Problem: 3240
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:7980 ms
7 Memory:3232 kb
8 ****************************************************************/
9
10 //BZOJ 3240
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1e6+10,INF=~0u>>2,P=1e9+7;
29 typedef long long LL;
30 /******************tamplate*********************/
31
32 struct Matrix{
33 int v[2][2];
34 Matrix(int x=0){F(i,0,1)F(j,0,1)if(i==j)v[i][j]=x;else v[i][j]=0;}
35 int* operator [] (int x){return v[x];}
36 }s1,s2,v;
37 inline Matrix operator * (Matrix a,Matrix b){
38 Matrix c;
39 if (a[0][1]==0 && a[1][1]==1 && b[0][1]==0 && b[1][1]==1){
40 c[0][0]=(LL)a[0][0]*b[0][0]%P;
41 c[0][1]=0;
42 c[1][0]=((LL)a[0][0]*b[1][0]+(LL)a[1][0])%P;
43 c[1][1]=1;
44 return c;
45 }
46 F(k,0,1) F(i,0,1) F(j,0,1)
47 c[i][j]=((LL)c[i][j]+(LL)a[i][k]*b[k][j]%P)%P;
48 return c;
49 }
50 inline Matrix Pow(Matrix a,int b){
51 Matrix c(1);
52 F(i,1,b) c=c*a;
53 return c;
54 }
55 inline Matrix Power(Matrix a,char* s){
56 Matrix r(1); int l=strlen(s);
57 D(i,l-1,0){
58 if (s[i]-‘0‘) r=r*Pow(a,s[i]-‘0‘);
59 a=Pow(a,10);
60 }
61 return r;
62 }
63 char n[N],m[N];
64 int main(){
65 #ifndef ONLINE_JUDGE
66 freopen("3240.in","r",stdin);
67 freopen("3240.out","w",stdout);
68 #endif
69 scanf("%s",n); scanf("%s",m);
70 int l1=strlen(n)-1;
71 while(n[l1]==‘0‘) n[l1--]=‘9‘;
72 n[l1]--;
73 l1=strlen(m)-1;
74 while(m[l1]==‘0‘) m[l1--]=‘9‘;
75 m[l1]--;
76 // printf("%s %s\n",n,m);
77 int a,b,c,d;
78 a=getint(); b=getint(); c=getint(); d=getint();
79 v[0][0]=v[0][1]=1; v[1][0]=v[1][1]=0;
80 s1[0][0]=a; s1[0][1]=0; s1[1][0]=b; s1[1][1]=1;
81 s2[0][0]=c; s2[0][1]=0; s2[1][0]=d; s2[1][1]=1;
82 Matrix s3=Power(s1,m);
83 v=v*(Power(s3*s2,n)*s3);
84 printf("%d\n",v[0][0]);
85 return 0;
86 }