#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll INF=1LL<<60; const int inf=1<<30; const int mod=1e9+7; const int MOD=998244353; const int nmax=200005; ll fac[nmax],finv[nmax],inv[nmax]; void COMinit(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i<nmax;i++){ fac[i]=fac[i-1]*i%mod; inv[i]=mod-inv[mod%i]*(mod/i)%mod; finv[i]=finv[i-1]*inv[i]%mod; } } ll com(int n,int k){ if(n<k||n<0||k<0){ return 0; } return fac[n]*(finv[k]*finv[n-k]%mod)%mod; } ll pow_mod(ll n,ll k,ll m){ ll res=1; for(;k>0;k>>=1){ if(k&1){ res=(res*n)%m; } n=(n*n)%m; } return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n,m;cin >> n >> m; if(m>n){ cout << 0 << endl; } vector<ll> t(n+1); t[0]=1; for(int i=1;i<=n;i++){ (t[i]=t[i-1]*n)%=mod; } COMinit(); ll ans=0; for(ll i=m;i>=1;i--){ ll tmp=pow_mod(i,n,mod)*com(m,i)%mod; if((m-i)%2==0){ (ans+=tmp)%=mod; } else{ (ans+=-tmp+mod)%=mod; } } cout << ans << endl; }