#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rl register ll
#define ws putchar(' ')
#define wl putchar('\n')
#define fos(i, a, b) for(rl i=a; i <= b; ++ i)
#define fop(i, a, b) for(rl i=a; i >= b; -- i)
template <class T> inline void read(T &res)
{
char ch = getchar(); bool f = 0; res = 0;
for(; !isdigit(ch); ch = getchar()) f |= ch == '-';
for(; isdigit(ch); ch = getchar()) res = (res << 1) + (res << 3) + (ch ^ 48);
res = f ? ~res + 1 : res;
}
template <class T> inline void waw(T x)
{
if(x > 9) waw(x / 10);
putchar(x % 10 + '0');
}
template <class T> inline void ww(T x)
{
if(x < 0) x = ~x + 1, putchar('-');
waw(x);
}
const ll N = 2;
ll n, m, a1, a2, p, q;
struct Matrix
{
ll a[N][N], m;
Matrix() {memset(a, 0, sizeof a); }
inline void init(ll x, ll b, ll c, ll d, ll _m)
{
a[0][0] = x, a[0][1] = b, a[1][0] = c, a[1][1] = d; m = _m;
}
Matrix operator *(const Matrix &x) const
{
Matrix res;
fos(i, 0, N - 1)
fos(k, 0, N - 1)
fos(j, 0, N - 1)
res.a[i][j] = (res.a[i][j] + (a[i][k] * x.a[k][j]) % m) % m;
return res;
}
} base, ans;
inline void qmi(ll k)
{
while(k)
{
if(k & 1) ans = ans * base;
base = base * base;
k >>= 1;
}
}
int main()
{
read(p), read(q), read(a1), read(a2), read(n), read(m);
if(n == 1) { waw(a1); return 0; }
else if(n == 2) {waw(a2); return 0; }
ll k = n - 2;
ans.init(a1, a2, 0, 0, m), base.init(0, q, 1, p, m);
qmi(k);
waw(ans.a[0][1]), wl;
return 0;
}