#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int MAXN = 1e2 + 10;
const int INF = 0x3f;
int mod;
struct node{
int p[3][3];
node(){
memset(p, 0, sizeof(p));
}
};
int m, ag, c, x0, n, g;
inline int read()
{
register char ch = getchar();
register int x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' and ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
inline int smul(int a, int b, int k){
int res = 0;
while (b){
if (b & 1){
res += a;
res %= k;
}
a <<= 1;
a %= k;
b >>= 1;
}
return res;
}
node operator*(const node& g, const node& x){
node l;
for (int i = 1; i <= 2; i++){
for (int j = 1; j <= 2; j++){
for (int f = 1; f <= 2; f++){
l.p[i][j] = (l.p[i][j] + smul(g.p[i][f], x.p[f][j], mod)) % mod;
}
}
}
return l;
}
int main()
{
mod = read();
ag = read();
c = read();
x0 = read();
n = read();
g = read();
node ans, a;
ans.p[1][1] = x0;
ans.p[1][2] = c;
a.p[1][1] = ag;
a.p[2][1] = 1;
a.p[2][2] = 1;
while (n > 0)
{
if (n & 1)
{
ans = ans * a;
}
a = a * a;
n >>= 1;
}
cout << (ans.p[1][1]) % g;
return 0;
}
https://www.luogu.com.cn/problem/P2044