记录
#include <bits/stdc++.h>
using namespace std;
#define hh putchar('\n')
#define kg putchar(' ')
#define i128 __int128
namespace quickread{
inline i128 read(){
i128 x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void write(i128 x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
}
using namespace quickread;
const i128 N=1e6+10;
i128 n,a,c,mod,g,x0;
struct csp{
i128 m[3][3];
csp(){memset(m,0,sizeof(m));}
void build(){m[1][1]=a;m[2][1]=0;m[1][2]=m[2][2]=1;}
};
struct node{
i128 m[3][2];
node(){memset(m,0,sizeof(m));}
void build(){m[1][1]=x0;m[2][1]=c;}
};
node operator * (const node & x, const csp & y){
node ans;
for(i128 i=1;i<=2;i++)
for(i128 j=1;j<=2;j++)
for(i128 k=1;k<=1;k++)
ans.m[j][k]=(ans.m[j][k] + x.m[j][k] * y.m[i][j] % mod) % mod;
return ans;
}
csp operator * (const csp & x,const csp &y){
csp ans;
for(i128 i=1;i<=2;i++)
for(i128 j=1;j<=2;j++)
for(i128 k=1;k<=2;k++)
ans.m[i][j]=(ans.m[i][j] + x.m[i][j] * y.m[j][k] % mod) %mod;
return ans;
}
signed main(){
mod=read(),a=read(),c=read(),x0=read(),n=read(),g=read();
csp ans,a1;a1.build();
ans.m[1][1]=ans.m[2][2]=1;
while(n){
if(n&1) ans=ans*a1;
a1=a1*a1;
n>>=1;
}
node other;other.build();
node anss=other*ans;
write(anss.m[1][1]%g);
return 0;
}