#include <iostream>
#include <algorithm>
#include<map>
using namespace std;
typedef long long ll;
const ll maxn=1e2+10,mod=1e9+7;
ll M,aa,c,x0,n,g;
ll llm(ll x,ll y){
ll res=0;
while(y){
if(y&1)res=(res+x)%M;
x=(x+x)%M;
x%=mod;
res%=mod;
y>>=1;
}
return res;
}
struct mat{
ll a[maxn][maxn],n,m;
mat(){
memset(a,0,sizeof(a));
n=0,m=0;
}
mat operator*(const mat &b)const{
mat res;
res.n=n,res.m=b.m;
for(int i=1;i<=n;i++){
for(int j=1;j<=b.m;j++){
for(int k=1;k<=m;k++){
res.a[i][j]=(res.a[i][j]+llm(a[i][k],b.a[k][j]))%M;
res.a[i][j]%=mod;
}
}
}
return res;
}
mat operator^(ll x)const{
mat tmp=*this;
if(x==1)return tmp;
mat res=tmp^(x>>1);
if(x%2==1)return res*res*tmp;
else return res*res;
}
void out(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<a[i][j]<<" \n"[j==m];
}
}
}
}a,k;
void init(){
a.a[1][1]=x0;
a.a[2][1]=c;
a.n=2,a.m=1;
k.a[1][1]=aa;
k.a[1][2]=1;
k.a[2][2]=1;
k.n=2,k.m=2;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
mat();
cin>>M>>aa>>c>>x0>>n>>g;
init();
cout<<((k^(n))*a).a[1][1]%g<<endl;
return 0;
}