#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int maxn = 5e5 + 5;
ll n, m, inv[maxn], fac[maxn], ans;
ll qpow(ll a, ll b) {
	ll ans = 1, base = a;
	while (b) {
		if (b & 1) ans = ans * base % MOD;
		b >>= 1, base = base * base % MOD;
	}
	return ans;
}
void init() {
	fac[0] = 1;
	for (int i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % MOD;
	for (int i = 0; i <= m; i++) inv[i] = qpow(fac[i], MOD - 2);
}
ll A(ll a, ll b) {
	return fac[a] * inv[a - b] % MOD;
}
ll C(ll a, ll b) {
	return fac[a] * inv[b] % MOD * inv[a - b] % MOD;
}
int main() {
	cin >> n >> m;
	init();
	ans = qpow(A(m, n),2);
	for (ll s = 1; s <= n; s++) {
		ans -= (s & 1 ? 1 : -1) * C(n, s) * A(m, s) % MOD * qpow(A(m - s, n - s),2);
		ans%=MOD;
	}
	cout<<(ans+MOD)%MOD;
	return 0;
}
