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