import numpy as np M, K = map(int, input().split()) MOD = 10 ** 9 + 7 A = np.zeros((M, M), dtype=np.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=10 ** 9 + 7): if not n: return np.eye(M, dtype=np.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])