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: Wrong Answer)

#include <bits/stdc++.h> 
using namespace std; 

const long long N = 500005;
const long long mod = 1000000007;
long long fact[N]; 

void set_fact() {
	fact[0] = 1; for (long long i = 1; i < N; i ++) fact[i] = (fact[i-1] * i) % mod;
}

long long power(long long x, long long y) {
    long long ans = 1;
  	while (y > 0) {
        if (y % 2 == 1) ans = (ans*x) % mod;
        y /= 2; x = (x*x) % mod;  
    } return ans % mod;
}

long long nCr(long long n, long long r) {
	return (fact[n] * power(fact[n - r], mod - 2)) % mod * power(fact[r], mod - 2) % mod;
}

int main() {
    set_fact(); long long n, m; cin >> n >> m; 
    long long fact_arr[n+1]; for (long long i = 0; i <= n; i ++) fact_arr[i] = nCr(n, i);
    long long sgn_arr[n+1]; sgn_arr[0] = 1; for (long long i = 1; i <= n; i++) sgn_arr[i] = sgn_arr[i-1] * (-1);
    long long perm_arr[n+1]; perm_arr[0] = 1;
    for (long long i = 1; i <= n; i ++) perm_arr[i] = (perm_arr[i-1] * ((m-n)+i)) % mod;
    long long ans = 0;
    for (long long fill = 0; fill <= n; fill ++) {
        long long prod = fact_arr[fill] * sgn_arr[fill] * perm_arr[n-fill];
        ans = (ans + prod) % mod;
    } ans = (ans * perm_arr[n]) % mod;
    cout << ans << endl;
}