z=10**9+7 def c(n,r,p=z): return g[n]*h[r]*h[n-r]%p N,M=map(int,input().split()) g=[1,1] h=[1,1] v=[0,1] for i in range(2,N+1): g.append(g[-1]*i%z) v.append(-v[z%i]*(z//i)%z) h.append(h[-1]*v[-1]%z) P=0 for i in range(M): P+=pow(-1,i)*pow(M-i,N,z)*c(M,M-i) print(P%z)