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 <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long
#define MAX_N 500001

using namespace std;

long long MOD = 1000000007;

long long fact[MAX_N];
void factorial(int N){
    fact[0]=1;
    for(int i=0; i<N; i++){
        fact[i+1]=(i+1)*fact[i]%MOD;
    }
}

map<long long, long long> inv;
long long inverse(long long x){
    if (inv.count(x)){
        return inv[x];
    }else if(x==0){
        return 1;
    }

    long long p=MOD-2;
    long long ret=1;
    long long tmp=x;
    while(p>0){
        if(p%2==1)ret=ret*tmp%MOD;
        tmp=tmp*tmp%MOD;
        p/=2;
        //cout << p << endl;
    }
    inv[x]=ret;
    return ret;
}

long long comb(int a, int b){
    long long ret=fact[a]*inverse(fact[a-b])%MOD;
    return ret*inverse(fact[b])%MOD;
}

int main(){
    ll N, M;
    cin >> N >> M;

    //cout << N << ' ' << M;
    factorial(M);
    ll ans=0;
    ll f=1;
    for(ll K=1; K<=N; K++){
        ll tmp=comb(N,K)*fact[M]%MOD;
        tmp=(tmp*inverse(fact[M-K]))%MOD;
        tmp=(tmp*fact[M-K])%MOD;
        tmp=(tmp*fact[M-K])%MOD;
        tmp=(tmp*inverse(fact[M-N]))%MOD;
        tmp=(tmp*inverse(fact[M-N]))%MOD;
        ans+=f*tmp;
        ans%=MOD;
        f*=-1;
    }

    ll tmp = fact[M]*inverse(fact[M-N])%MOD;
    tmp =(tmp*tmp)%MOD;
    //cout << tmp << endl;
    //cout << ans << endl;
    cout << ((tmp-ans)%MOD+MOD)%MOD << endl;
}