import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] md = 10 ** 9 + 7 def dot(aa,bb): h=len(aa) w=len(bb[0]) res=[[0]*w for _ in range(h)] for i,row in enumerate(aa): for j,col in enumerate(zip(*bb)): res[i][j]=sum(a*b%md for a,b in zip(row,col))%md return res def main(): m,k=MI() base=[[0]*m for _ in range(m)] for i in range(m): for a in range(m): j=(a+i)%m base[i][j]+=1 j=(a*i)%m base[i][j]+=1 mat=[[0]*m for _ in range(m)] for i in range(m):mat[i][i]=1 while k: if k&1:mat=dot(mat,base) base=dot(base,base) k>>=1 #print(mat) print(mat[0][0]) main()