#include<bits/stdc++.h>
using namespace std;
#define ll long long

const int mod = 1e9+7;
const int n = 1e6;
ll fact[n+1], fin[n+1];

ll powe(ll x, ll e) {
    ll r = 1;
    while(e) {
        if(e&1) r = r * x % mod;
        x = x * x % mod;
        e >>= 1;
    }
    return r;
}

void init() {
    fact[0] = 1;
    for(int i=1;i<=n;i++) fact[i] = fact[i-1] * i % mod;
    fin[n-1] = powe(fact[n-1], mod-2);
    for(int i=n-2;i>=0;i--) fin[i]= fin[i+1] * (i+1) % mod;
}

ll choose(int a, int b) {
	return fact[a] * fin[b] % mod * fin[a - b] % mod;
}

ll permute(int a, int b) {
	return fact[a] * fin[a - b] % mod;
}

int main(){
    init();
    int n, m;
    cin >> n >> m;
    ll res = 0;
    // PIE
    for(int i=0;i<=n;i++) {
        int c = i%2 ? -1 : 1;
        ll add = c * choose(n, i) % mod * permute(m, i) % mod * permute(m-i, n-i) % mod * permute(m-i, n-i) % mod;
        add = (add + mod) % mod;
        res = (res + add) % mod;
    }
    cout << res << endl;
}
