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