import re import sys content = input() pattern = r'^(\d+) (\d+) (\d+) (\d+) (\d+)$' result = re.match(pattern, content) if not result: sys.exit("format error.") N, M, L, K, B = map(int, result.groups()) if not (1 <= N and N <= 10**18): sys.exit("N") if not (1 <= M and M <= 10**18): sys.exit("M") if not (1 <= L and L <= 10**3): sys.exit("L") if not (0 <= K and K < L): sys.exit("K") if not (1 <= B and B <= 10**9): sys.exit("B") if L == 1: print(pow(M + 1, N, B)) exit() # 途中で mod を取らずに計算.これは TLE か MLE してほしい. def mul(f, g): h = [0] * (2 * L) for i in range(L): for j in range(L): h[i + j] += f[i] * g[j] for i in range(L): h[i] += h[i + L] * M return h f = [0] * L f[0] = 1 p = [0] * L p[0] = 1 p[1] = 1 while N > 0: if N % 2 == 1: f = mul(f, p) p = mul(p, p) N //= 2 print(f[K] % B)