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 <iostream>
using namespace std;

long long mod = 1000000007;
long long fact[1 << 19], inv[1 << 19];
long long val[1 << 19];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 32; i++) {
		if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Div(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

void init() {
	fact[0] = 1;
	for (int i = 1; i <= 500000; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 0; i <= 500000; i++) inv[i] = Div(1, fact[i], mod);

	val[0] = 1;
	for (int i = 1; i <= 500000; i++) {
		val[i] = (1LL * i * val[i - 1]);
		if (i % 2 == 1) val[i] -= 1LL;
		if (i % 2 == 0) val[i] += 1LL;
		val[i] = (val[i] + mod * mod) % mod;
	}
}

long long ncr(int n, int r) {
	if (n < r || r < 0) return 0;
	return (fact[n] * inv[r] % mod) * inv[n - r] % mod;
}

long long N, M;
long long Answer;

int main() {
	cin >> N >> M; init();

	int cx = M - N, cy = N;
	if (cx == 0) {
		Answer = val[N];
	}
	else {
		for (int i = 1; i <= cy; i++) {
			long long v1 = ncr((cx - 1) + (cy - i), cy - i);
			long long v2 = fact[cy];
			long long v3 = inv[i];
			long long v4 = val[i];
			Answer += (v1 * v2 % mod) * (v3 * v4 % mod) % mod;
			Answer %= mod;
		}
		for (int i = 1; i <= cx; i++) {
			long long v1 = ncr((cx - i) + (cy - 1), cy - 1);
			long long v2 = fact[cy];
			Answer += v1 * v2 % mod;
			Answer %= mod;
		}
	}

	long long F1 = fact[M];
	long long F2 = inv[M - N];
	long long F = F1 * F2 % mod;
	cout << Answer * F % mod << endl;
	return 0;
}