p = 10**9+7 def my_pow(b,e): ret = 1 while e: if e&1: ret = ret * b % p b = b * b % p e>>=1 return ret N,M = map(int,raw_input().split()) F = [1]; FI = [0]*(M+1) for i in xrange(1,M+1):F += [i*F[i-1]%p] FI[M] = my_pow(F[M],p-2) for i in xrange(M,0,-1):FI[i-1] = i*FI[i]%p print sum(F[M]*FI[k]*FI[M-k]*my_pow(M-k,N)*((-1)**(k%2)) for k in xrange(M))%p