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;

#define int long long

#define x first
#define y second
#define pb push_back
#define mp make_pair

#define all(a) (a).begin(), (a).end()

const int M = 1e9 + 7;
const int N = 5e5 + 7;

int f[N];

int pw(int a, int n) {
    int b = 1;
    while (n > 0) {
        if (n & 1) b = b * a % M;
        a = a * a % M;
        n /= 2;
    }
    return b;
}

int cnk(int n, int k) {
    return f[n] * pw(f[k], M - 2) % M * pw(f[n - k], M - 2) % M;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    f[0] = 1;
    for (int i = 1; i < N; i++) f[i] = f[i - 1] * i % M;
    int n, m;
    cin >> n >> m;
    int ans = cnk(m, n) * f[n] % M * cnk(m, n) % M * f[n] % M;
    ans %= M;
    int bad = 0;
    for (int i = 1; i <= n; i++) {
        int ways = cnk(n, i);
        ways *= cnk(m, i);
        ways %= M;
        ways *= f[i];
        ways %= M;
        ways *= cnk(m - i, n - i);
        ways %= M;
        ways *= f[n - i];
        ways %= M;
        ways *= cnk(m - i, n - i);
        ways %= M;
        ways *= f[n - i];
        ways %= M;
        if (i & 1) {
            bad += ways;
            if (bad >= M) bad -= M;
        } else {
            bad -= ways;
            if (bad < 0) bad += M;
        }
    }
    ans -= bad;
    if (ans < 0) ans += M;
    cout << ans;
    return 0;
}