/***********************************************************
	When you are inspired by passion 
	and you want to live for the current moment

	When your every thought and action
	has the power to become the story of your future

	When difficulties stand to demolish your Glory
	Just remember that Life is a 'Golden adverse Story'

	Withstand it to acheive your passion and dreams
	more amazingly more easily.

	Why then? When writing the story of your Life
	You want to Let someone else hold your pen

					huzaifa242©

***********************************************************/
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define MAX 500005
#define x first 
#define y second

int seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = numeric_limits<int>::max())
{return uniform_int_distribution<int> (l, r)(rng);}

template <typename T, typename G>
ostream& operator << (ostream& out, const pair<T, G> &a) 
{return out << "( " << a.x << ", " << a.y << ")";}

template <typename T>
ostream& operator << (ostream& out, const vector<T> &a) 
{out << "[ "; for(const auto &i: a) out << i << ", "; return out << "]" << endl;}

template <typename T>
ostream& operator << (ostream& out, const set<T> &a) 
{out << "{ "; for(const auto &i: a) out << i << ", "; return out << "}" << endl;}

template <typename T, typename G>
ostream& operator << (ostream& out, const map<T, G> &a) 
{out << "< "; for(const auto &i: a) out << i << ", "; return out << ">" << endl;}

template<typename T, size_t N>
typename enable_if<!is_same<typename remove_cv<T>::type, char>::value,
ostream&>::type operator << (ostream& out, T (&a)[N])
{out << "[ "; for(const auto &i: a) out << i << ", "; return out << "]" << endl;}

void debug_out(){cerr << endl;} 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{cerr << (H) << ' '; debug_out(T...);}
 
#ifdef LOCAL
#define debug(...) cerr << '#' << #__VA_ARGS__ << ":\n", debug_out(__VA_ARGS__)
#else
#define debug(...) 24
#endif

const int mod = 1e9 + 7;
int md(int x,int mdo=mod){
	x%=mdo;
	return x<0?x+mdo:x;
}
int multi(int a, int b, int mdo=mod){
	int pro=0;
	a=md(a,mdo);
	while(b){
		if(b&1)
			pro=md(md(pro,mdo)+md(a,mdo),mdo);
		a=md(a*2,mdo);
		b>>=1;
	}
	return pro;
}
int pwr(int a,int n,int mdo=mod){
	if(n==0)
		return md(1,mdo);
	else if(n==1)
		return md(a,mdo);
	int x=pwr(a,n/2,mdo);
	if(max(x,md(a,mdo))< int(1e9))
		x=md(x*x,mdo);
	else
		x=md(multi(x,x,mdo),mdo);
	if(n&1){
		if(max(x,md(a,mdo))< int(1e9))
			x=md(x*md(a,mdo),mdo);
		else
			x=md(multi(x,md(a,mdo),mdo),mdo);
	}
	return x;
}
int ge(int a, int b, int *x, int *y){
	if (a == 0){
		*x = 0, *y = 1;
		return b;
	}
	int x1, y1;
	int gcd = ge(b%a, a, &x1, &y1);
	*x = y1 - (b/a) * x1;
	*y = x1;
	return gcd;
}
int  modinv(int a, int m=mod){
	int x, y;
	int g = ge(a, m, &x, &y);
	// if g!=1 modinv doesn't exist;
	assert(g==1);
	int p = (x%m + m) % m;
	return p;
}
vector<int> fct,ifct;
void factorials(int n){
	int i;
	fct.resize(n+1);
	ifct.resize(n+1);
	fct[0]=1;
	for(i=1;i<=n;i++){
		fct[i]=md(fct[i-1]*i);
	}
	ifct[n]=modinv(fct[n]);
	for(i=n;i>0;i--){
		ifct[i-1]=md(ifct[i]*i);
	}
}
int ncr(int n, int r){
	if(n<0 || r<0 || n<r)
		return 0;
	return md(fct[n]*md(ifct[r]*ifct[n-r]));
}
int npr(int n, int r) {
	return md(ncr(n, r)*fct[r]);
}
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	factorials(MAX);
	int ans = 0, n, m;
	cin >> n >> m;
	for (int i = 0; i <= n; ++i) {
		int cur = md(npr(m,i)*md(ncr(n,i)*md(pwr(npr(m-i,n-i),2))));
		if (i&1)
			ans = md(ans - cur);
		else
			ans = md(ans + cur);  
	} 
	cout << ans << '\n';
	return 0;
}


Measured memory_bytes: 11024