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++ (Clang 10.0.0) (judge verdict: Accepted)

#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>
#define rep(i, x) for(ll i = 0; i < x; i++)
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9+7;
const ll N = 5e5+5, inf = 1e9+5000;
ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; }
ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; }
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll mod_exp(ll x, ll e) {
  ll ans = 1;
  while (e > 0) {
    if (e & 1) ans = mult(ans, x);
    x = mult(x, x);
    e >>= 1;
  }
  return ans;
}

ll inv(ll x) {
  return mod_exp(x, MOD-2);
}

ll fact[N], inv_fact[N];

ll ncr(ll n, ll k) {
  return mult(fact[n], mult(inv_fact[n-k], inv_fact[k]));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll N, M; cin >> N >> M;
    fact[0] = 1;
    inv_fact[0] = 1;
    for (ll i = 1; i < M+5; i++) {
      fact[i] = mult(fact[i-1], i);
      inv_fact[i] = inv(fact[i]);
    }

    ll no = 0;
    for (ll i = 1; i <= N; i++) {
      ll curr = ncr(N, i);
      curr = mult(curr, mod_exp(MOD-1, i+1));
      curr = mult(curr, mod_exp(mult(fact[M-i], inv_fact[M-N]), 2));
      curr = mult(curr, mult(fact[M], inv_fact[M-i]));
      no = add(no, curr);
    }
    ll all = mod_exp(mult(fact[M], inv_fact[M-N]), 2);
    cout << sub(all, no) << endl;
}