#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
typedef long long ll;
ll MOD=1000000007;
string N,M;
ll n,m,a,b,c,d;
ll A[2][2],B[2][2],C[2][2],D[2][2],u[2][2];
void J(ll x[2][2],ll y[2][2])
{
for(int i=0;i<2;i++)
for(int j=0;j<2;j++){
ll uu=0;
for(int k=0;k<2;k++)
uu=(uu+x[i][k]*y[k][j]%MOD)%MOD;
u[i][j]=uu%MOD;
}
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
x[i][j]=u[i][j];
}
ll Mod(string s,ll x)
{
ll ans=0;
for(int i=0;i<s.size();i++)
ans=(ans*10+s[i]-'0')%x;
return ans;
}
void solve()
{
cin>>N>>M;
cin>>a>>b>>c>>d;
//cout<<N<<" "<<M<<" "<<a<<b<<c<<d<<endl;
if(c==1) n=Mod(N,MOD); else n=Mod(N,MOD-1);
if(a==1) m=Mod(M,MOD); else m=Mod(M,MOD-1);
//cout<<n<<m;
n--;m-=2;
A[0][0]=1,A[0][1]=1,A[1][0]=0,A[1][1]=0;
B[0][0]=a,B[0][1]=0,B[1][0]=b,B[1][1]=1;
C[0][0]=c,C[0][1]=0,C[1][0]=d,C[1][1]=1;
D[0][0]=a,D[0][1]=0,D[1][0]=b,D[1][1]=1;
while(m)
{
if(m&1)
J(B,D);
J(D,D);
m>>=1;
}
D[0][0]=B[0][0],D[0][1]=B[0][1],D[1][0]=B[1][0],D[1][1]=B[1][1];
J(B,C);
while(n)
{
if(n&1)
J(A,B);
J(B,B);
n>>=1;
}
J(A,D);
cout<<A[0][0]<<endl;
}
int main()
{
IOS;
solve();
return 0;
}