import sys input = sys.stdin.readline def Matprod(A, B, mod, N): temp = [0] * N*N for i in range(N): for j in range(N): ij = i * N + j for k in range(N): temp[ij] += A[i*N+k] * B[k*N+j] temp[ij] %= mod return temp def Matpow_Linear(A, M, mod, N): Mat = [0] * N*N for i in range(N): Mat[i*N+i] = 1 while M: if M & 1: Mat = Matprod(Mat, A, mod, N) A = Matprod(A, A, mod, N) M >>= 1 return Mat M, K = map(int, input().split()) A = [0] * (M*M) for i in range(M): for j in range(M): nex = (i+j)%M A[nex*M+i] += 1 nex = (i*j)%M A[nex*M+i] += 1 mod = 10 ** 9 + 7 A = Matpow_Linear(A, K, mod, M) print(A[0]%mod)