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