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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define maxn 500005
#define mod 1000000007
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vll;
typedef vector<bool> vb;
typedef set<int> si; 
#define fori(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,b,a) for(int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
#define __builtin_popcountll bitCountLL
#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
const int INF = 2e9;
const ll ML = 4e18;
ll Pow(ll x, ll p) {
    ll res = 1;
    while(p) {
        if(p & 1) res = (res * x) % mod;
        p >>= 1;
        x = (x * x ) % mod;
    }
    return res;
}
ll fac[maxn], inv[maxn];
void init() {
    fac[0] = fac[1] = 1;
    fori(i,2,maxn) fac[i] = (fac[i-1] * i) % mod;
    inv[maxn-1] = Pow(fac[maxn-1],mod-2);
    rep(i,maxn-2,0) inv[i] = (inv[i+1] * (i + 1)) % mod;
}
ll C(int a, int b) {
    return (fac[b] * inv[a] % mod * inv[b - a] % mod) % mod;
}
ll A(int a, int b) {
    return fac[b] * inv[b-a] % mod;
}
void solve() {
    int n,m;
   cin >> n >> m;
   //cout << A(n,m);
    ll ans = 0, flag = 1;
    fori(i,0,n+1) {
        ans = (mod + ans + flag * (A(n-i,m-i) * C(i,n) % mod)) % mod;
        flag *= -1;
    }
    ans = ans * A(n,m) % mod;
    cout << ans << endl;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
	//cout.precision(30); cout << fixed;
	//freopen("D:\\data_in.txt", "r", stdin);
    init();
   solve();
    
    /*clock_t start,finish;
    start = clock();
   
    finish = clock();
    cout << "\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 << "ms" << endl;
    */
    return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/