import numpy as np M, K = map(int, input().split()) MOD = 10 ** 9 + 7 A = np.zeros((M, M), dtype=object) for i in range(M): for j in range(M): A[i, (i + j) % M] += 1 A[i, (i * j) % M] += 1 def pow_matrix_mod(x, n, mod=MOD): x = x.astype(object) if not n: return np.eye(len(x), dtype=object) if n % 2 == 0: return pow_matrix_mod(x @ x % mod, n // 2) % mod else: return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod print(pow_matrix_mod(A, K)[0, 0])