def eea(a,b):
    if b==0:
        return (1,0)
    else:
        q = a // b
        r = a % b
        s,t = eea(b,r)
        return (t, s - q * t)

p, q, e = map(int, tuple(input().split()))
E = int(input())
N = p * q
d = eea(e,(p-1)*(q-1))[0]
M = pow(E, d, N)
print(M)
