Problem:
Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

A_i \neq B_i, for every i such that 1\leq i\leq N.

A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

1\leq N \leq M \leq 5\times10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count modulo (10^9+7).

Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.

Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=1e18;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 500005
using namespace std;
ll fact[N],invf[N];
long long powmod(long long a,long long po){
	if(po==0)return 1;
	if(po==1)return a;
	long long int p1=powmod(a,po/2ll);
	long long int p2=p1*p1;
	long long p3=powmod(a,po%2ll);
	return ((p2%mod)*(p3%mod))%mod;
}
ll ncr(ll n,ll r){
	return fact[n]*invf[r]%mod*invf[n-r]%mod;
}
void solve(){
	ll n,m;
	cin>>n>>m;
	fact[0]=invf[0]=fact[1]=invf[1]=1;
	for(ll i=2;i<=m;i++){
		fact[i]=fact[i-1]*i%mod;
		invf[i]=invf[i-1]*powmod(i,mod-2)%mod;
	}
	ll ans=ncr(m,n)*ncr(m,n)%mod*fact[n]%mod*fact[n]%mod;
	for(ll i=1;i<=n;i++){
		ll nci=ncr(n,i);
		ll mci=ncr(m,i);
		ll micni=ncr(m-i,n-i);
		ans=ans+powmod(-1ll,i)*nci%mod*mci%mod*fact[i]%mod*micni%mod*micni%mod*fact[n-i]%mod*fact[n-i]%mod;
		ans%=mod;
	}
	ans+=mod;
	ans%=mod;
	cout<<ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}