M,K = map(int,input().split()) P = 10 ** 9 + 7 def seki(x,y): l = [[0] * M for _ in range(M)] for i in range(M): for j in range(M): for k in range(M): l[i][j] = (l[i][j] + x[i][k] * y[k][j]) % P return l A = [[0] * M for _ in range(M)] tmp = [[0] * M for _ in range(M)] for i in range(M): tmp[i][i] = 1 for i in range(M): for j in range(M): A[(i+j)%M][i] += 1 A[i*j%M][i] += 1 while K: if K & 1: tmp = seki(tmp,A) A = seki(A,A) K >>= 1 print(tmp[0][0])