/*
再来个题练练手
scanf longlong 有bug....... */
#include<cstdio>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
ll n,m,x,y,x0,g;
ll f[][],a[][];
ll slow_mul(ll a,ll b,ll c)
{
ll ans=;
a=a%c;b=b%c;
while(b)
{
if(b&)
{
b--;
ans+=a;
ans%=c;
}
a<<=;a%=c;b>>=;
}
return ans;
}
void mul(ll a[][],ll b[][])
{
ll c[][];
memset(c,,sizeof(c));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
for(int k=;k<=;k++)
c[i][j]=(c[i][j]+slow_mul(a[i][k],b[k][j],m))%m;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
a[i][j]=c[i][j];
}
int main()
{
cin>>m>>x>>y>>x0>>n>>g;
//scanf("%ld%ld%ld%ld%ld%ld",&m,&x,&y,&x0,&n,&g);
f[][]=x0;f[][]=;a[][]=x;a[][]=;a[][]=y;a[][]=;
while(n)
{
if(n&)mul(f,a);
mul(a,a);
n>>=;
}
printf("%ld\n",f[][]%g);
return ;
}