import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### #2D matrix def E(n): A = [[0 for j in range(n)]for i in range(n)] for i in range(n): A[i][i] = 1 return A def add(x,y): return (x + y)%mod def mul(x, y): return x * y % mod def mat_add(A, B, replace=False): assert len(A)==len(B) and len(A[0]) == len(B[0]) if not replace: A = [a.copy() for a in A] n = len(A) m = len(A[0]) for i in range(n): for j in range(m): A[i][j] = add(A[i][j], B[i][j]) return A def mat_mul(A,B): assert len(A[0]) == len(B) n = len(A) m = len(B[0]) p = len(A[0]) R = [[0 for j in range(m)]for i in range(n)] for i in range(n): for j in range(m): for k in range(p): R[i][j] = add(R[i][j], mul(A[i][k],B[k][j])) return R def mat_pow(A, x): assert len(A)==len(A[0]) n = len(A) R = E(n) while x > 0: if x&1: R = mat_mul(R, A) A = mat_mul(A,A) x >>= 1 return R def mat_pri(A): for i in A: print(*i) n,m,l,k,b = na() mod = l * b a = [0] * l a[0] = 1 % b a[1 % l] = m % b f = [0] * l f[0] = 1 #print(f, a) while n: if n % 2: nf = [0] * l for i in range(l): for j in range(l): nf[(i+j)%l] += f[i] * a[j] % b nf[(i+j)%l] %= b f = nf na = [0] * l for i in range(l): for j in range(l): na[(i+j)%l] += a[i] * a[j] % b na[(i+j)%l] %= b a = na #print(f, a) n//=2 #print(f) print(f[k])