#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef LOCAL
freopen("rsir.in", "r", stdin);
freopen("rsir.out", "w", stdout);
cin.tie(nullptr)->sync_with_stdio(false);
#endif
int t0, t1, a, b, x, y, z, m; long long n;
cin >> t0 >> t1 >> a >> b >> x >> y >> z >> m >> n;
t0 %= m;
t1 %= m;
pair<int, int> testoasa{t0, t1}, iepure{t0, t1};
iepure = {iepure.second,
(1ll * a * iepure.first * iepure.first % m + 1ll * b * iepure.second * iepure.second % m +
x * iepure.first % m + y * iepure.second % m + z) % m};
while (testoasa != iepure) {
testoasa = {testoasa.second,
(1ll * a * testoasa.first * testoasa.first % m + 1ll * b * testoasa.second * testoasa.second % m +
x * testoasa.first % m + y * testoasa.second % m + z) % m};
iepure = {iepure.second,
(1ll * a * iepure.first * iepure.first % m + 1ll * b * iepure.second * iepure.second % m +
x * iepure.first % m + y * iepure.second % m + z) % m};
iepure = {iepure.second,
(1ll * a * iepure.first * iepure.first % m + 1ll * b * iepure.second * iepure.second % m +
x * iepure.first % m + y * iepure.second % m + z) % m};
}
int c = 0;
do {
testoasa = {testoasa.second,
(1ll * a * testoasa.first * testoasa.first % m + 1ll * b * testoasa.second * testoasa.second % m +
x * testoasa.first % m + y * testoasa.second % m + z) % m};
++c;
} while (testoasa != iepure);
testoasa = iepure = {t0, t1};
for (int i = 1; i <= c; ++i) {
iepure = {iepure.second,
(1ll * a * iepure.first * iepure.first % m + 1ll * b * iepure.second * iepure.second % m +
x * iepure.first % m + y * iepure.second % m + z) % m};
}
int p = 0;
while (testoasa != iepure) {
++p;
testoasa = {testoasa.second,
(1ll * a * testoasa.first * testoasa.first % m + 1ll * b * testoasa.second * testoasa.second % m +
x * testoasa.first % m + y * testoasa.second % m + z) % m};
iepure = {iepure.second,
(1ll * a * iepure.first * iepure.first % m + 1ll * b * iepure.second * iepure.second % m +
x * iepure.first % m + y * iepure.second % m + z) % m};
}
int target = p + int((n - p) % c);
testoasa = {t0, t1};
for (int i = 1; i < target; ++i) {
testoasa = {testoasa.second,
(1ll * a * testoasa.first * testoasa.first % m + 1ll * b * testoasa.second * testoasa.second % m +
x * testoasa.first % m + y * testoasa.second % m + z) % m};
}
cout << testoasa.second << '\n';
return 0;
}