#include <stdio.h>
#include <math.h>
#define maxn 7010
long t0, t1, v0, v1, w0, w1, a, b, x, y, z, mod, m, A[maxn], B[maxn];
long long n;
void next(long &t0, long &t1) {
long aux = t1;
t1 = A[t0] + B[t1];
if (t1 >= mod) {
t1 -= mod;
}
t1 += z;
if (t1 >= mod) {
t1 -= mod;
}
t0 = aux;
}
int main() {
freopen("rsir.in", "r", stdin);
freopen("rsir.out", "w", stdout);
scanf("%ld%ld%ld%ld%ld%ld%ld%ld%lld", &t0, &t1, &a, &b, &x, &y, &z, &mod, &n);
z %= mod;
if (n == 0) {
printf("%ld\n", t0);
return 0;
}
if (n == 0) {
printf("%ld\n", t1);
return 0;
}
long i;
for (i = 0; i < mod; ++i) {
A[i] = (1LL * i * i * a + x * i) % mod;
}
for (i = 0; i < mod; ++i) {
B[i] = (1LL * i * i * b + y * i) % mod;
}
v0 = w0 = t0;
w1 = v1 = t1;
do {
next(t0, t1);
next(w0, w1);
next(w0, w1);
}
while ((w0 != t0) || (w1 != t1));
do {
++m;
next(t0, t1);
}
while ((w0 != t0) || (w1 != t1));
--n;
while ((v0 != t0) || (v1 != t1)) {
--n;
next(v0, v1);
if (n == 0) {
printf("%ld\n", v1);
return 0;
}
}
n %= m;
for (i = 1; i <= n; ++i) {
next(v0, v1);
}
printf("%ld\n", v1);
return 0;
}