#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD=1e9+7,MAXN=5e5+10;
ll Fac[MAXN],InF[MAXN];
inline ll fpow(ll a,ll x){
    ll ans=1;
    for(;x;x>>=1,a=a*a%MOD) if(x&1) ans=ans*a%MOD;
    return ans;
}
inline void pre(){
    Fac[0]=1;
    for(int i=1;i<=5e5;i++)
        Fac[i]=Fac[i-1]*i%MOD;
    InF[int(5e5)]=fpow(Fac[int(5e5)],MOD-2);
    for(int i=5e5-1;i>=0;i--)
        InF[i]=InF[i+1]*(i+1)%MOD;
}
inline ll C(ll n,ll m) { return Fac[n]*InF[m]%MOD*InF[n-m]%MOD; }
inline ll A(ll n,ll m) { return Fac[n]*InF[n-m]%MOD; }
int main(){
    pre();
    ll N,M,Ans=0,Bas=1;
    cin>>N>>M;
    for(int i=0;i<=N;i++){
        Ans+=C(N,i)*A(M,i)%MOD*A(M-i,N-i)%MOD*A(M-i,N-i)%MOD*Bas;
        Ans=(Ans%MOD+MOD)%MOD;
        Bas*=-1;
    }
    cout<<Ans;
    return 0;
}