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