#include<bits//stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0;i < n;i++)
typedef long long ll;
ll n, m, x,sum = 0, ans = 0;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void COMint() {
    fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
ll COM(int n, int k) {
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll PER(int n, int k) {
    return (fac[n] * finv[n - k]) % MOD;
}
int main(void) {
    COMint();cin >> n >> m;
    rep(i, n + 1) {
        x = (PER(m - i, n - i) * PER(m - i, n - i)) % MOD;
        sum = (COM(n, i) * PER(m, i)) % MOD;
        sum = (sum * x) % MOD;
        if (i % 2 == 0) ans += sum,ans %= MOD;
        else ans = (ans - sum + MOD) % MOD;
    }cout << ans << endl;
}