def mat_mul(A,B): m=len(A) ans=[[0]*m for _ in range(m)] for i in range(m): for j in range(m): x=0 for k in range(m): x+=A[i][k]*B[k][j] ans[i][j]=x return ans def mat_mod(A,mod): m=len(A) for i in range(m): for j in range(m): A[i][j]%=mod return A def mat_pow(A,n,mod): m=len(A) ans=[[0]*m for _ in range(m)] for i in range(m): for j in range(m): if i==j: ans[i][j]=1 while n>0: if n%2==1: ans=mat_mul(ans,A) A=mat_mul(A,A) ans=mat_mod(ans,mod) n//=2 return ans from sys import stdin def main(): #入力 readline=stdin.readline M,K=map(int,readline().split()) mod=10**9+7 c=[[1]*M for _ in range(M)] for i in range(M): for j in range(M): for x in range(M): if i*x%M==j: c[i][j]+=1 c=mat_pow(c,K,mod) print(c[0][0]) if __name__=="__main__": main()