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)

#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };

template<typename T>
T power(T x, long long n, const T &m){
    if(n == 0) return 1;
    if(n == 1) return x;
    T tmp = power(x, n/2, m);
    if(n%2 == 0) return tmp * tmp % m;
    else return tmp * tmp % m * x % m;
}

template<typename T>
T inverse(T x, const T &m){
    return power(x, m-2, m);
}

struct Combination{
private:
    int N;
    long long M;
    vector<long long> f, i;
public:
    void init(int n, long long m){
        N = n; M = m;
        f.resize(N+1); f[0] = 1;
        for(long long j=1; j<=N; j++){
            f[j] = f[j-1] * j % M;
        }
        i.resize(N+1); i[N] = inverse(f[N], M);
        for(long long j=N-1; j>=0; j--){
            i[j] = i[j+1] * (j+1) % M;
        }
    }
    long long fact(int n){
        return f[n];
    }
    long long P(int n, int r){
        return f[n] * i[n-r] % M;
    }
    long long C(int n, int r){
        return f[n] * i[r] % M * i[n-r] % M;
    }
};

int N, M;
Combination C;

signed main(){
    cin >> N >> M;
    C.init(M, MOD);
    ll ans = 0;
    rep(i, N+1){
        if(i%2 == 0) ans += C.C(N, i) * C.P(M, i) % MOD
            * C.P(M-i, N-i) % MOD * C.P(M-i, N-i) % MOD;
        else ans -= C.C(N, i) * C.P(M, i) % MOD
            * C.P(M-i, N-i) % MOD * C.P(M-i, N-i) % MOD;
        ans = (ans + 2*MOD) % MOD;
    }
    cout << ans << endl;
}