#include <bits/stdc++.h>
#define LL long long

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

int n, m, fac[N] = { 1 }, inv[N] = { 1 };
LL ans;

int mul(int ta, int tb) {
	int ret;
	__asm__ __volatile__ ("\tmull %%ebx\n\tdivl %%ecx\n":"=d"(ret):"a"(ta), "b"(tb), "c"(mod));
	return ret;
}

int qpow(int bas, int po) {
	int res = 1;
	while(po) {
		if(po & 1)
			res = mul(res, bas);
		bas = mul(bas, bas);
		po >>= 1;
	}
	return res;
}

int C(int tn, int tm) {
	return mul(fac[tn], mul(inv[tm], inv[tn - tm]));
}

int A(int tn, int tm) {
	return mul(fac[tn], inv[tn - tm]);
}

int32_t main() {
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= m; ++i) {
		fac[i] = mul(fac[i - 1], i);
		inv[i] = qpow(fac[i], mod - 2);
	}
	for(int i = 0; i <= n; ++i) {
		int flg = (i & 1) ? -1 : 1;
		int tmp = mul(C(n, i), mul(A(m, i), qpow(A(m - i, n - i), 2)));
		ans += flg * tmp;
		ans %= mod, (ans += mod) %= mod;
	}
	printf("%lld", ans);
	return 0;
}