#include <bits/stdc++.h>
#define maxN 7002
#define ll long long
using namespace std;
short int k, p, ans, t0, t1, a, b, x, y, z, sqra[maxN], sqrb[maxN], mod;
ll n;
void read()
{
int i;
freopen("rsir.in", "r", stdin);
scanf("%d %d %d %d %d %d %d %d %lld", &t0, &t1, &a, &b, &x, &y, &z, &mod, &n);
z %= mod;
t0 %= mod;
t1 %= mod;
for (i = 1; i < mod; ++ i)
{
sqra[i] = (1LL * a * i * i + 1LL * x * i) % mod;
sqrb[i] = (1LL * b * i * i + 1LL * y * i + 1LL * z) % mod;
}
}
short int T(short int t0, short int t1)
{
t1 = sqra[t0] + sqrb[t1];
if (t1 >= mod)
t1 -= mod;
return t1;
}
void prec()
{
int f2 = t1, s2 = t1, f1 = t0, s1 = t0, aux;
do
{
aux = f2;
f2 = T(f1, f2);
f1 = aux;
aux = s2;
s2 = T(s2, T(s1, s2));
s1 = T(s1, aux);
}
while (f2 != s2 || f1 != s1);
int F1 = f1, F2 = f2;
f2 = T(f1, f2);
f1 = F2;
p = 1;
while (f2 != F2 || f1 != F1)
{
++ p;
int aux = f2;
f2 = T(f1, f2);
f1 = aux;
}
f1 = t0;
f2 = t1;
do
{
aux = f2;
f2 = T(f1, f2);
f1 = aux;
for (int i = 1; i <= p; ++ i)
{
aux = s2;
s2 = T(s1, s2);
s1 = aux;
}
}
while (f2 != s2 || f1 != s1);
F1 = f1;
F2 = f2;
f1 = t0;
f2 = t1;
k = 2;
while (f2 != F2 || f1 != F1)
{
++ k;
aux = f2;
f2 = T(f1, f2);
f1 = aux;
}
}
void solve()
{
ll pos = n;
prec();
if (pos > k)
pos = k - 1 + (pos - k + 1) % p;
if (pos == 0)
ans = t0;
else if (pos == 1)
ans = t1;
else
{
while (-- pos)
{
int aux = t1;
t1 = T(t0, t1);
t0 = aux;
}
ans = t1;
}
}
void write()
{
freopen("rsir.out", "w", stdout);
printf("%d\n", ans);
}
int main()
{
read();
solve();
write();
return 0;
}