m,k=map(int,input().split()) M=10**9+7 def seki(a,b): c=[[0]*m for i in range(m)] for i in range(m): for j in range(m): for k in range(m): c[i][j]+=a[i][k]*b[k][j] c[i][j]%=M return c A=[[0]*m for i in range(m)] for i in range(m): for j in range(m): A[(i+j)%m][i]+=1 A[(i*j)%m][i]+=1 B=[[int(j==i) for j in range(m)] for i in range(m)] for i in range(30): if (k>>i)&1: B=seki(A,B) A=seki(A,A) print(B[0][0])