/*Author - Shubham Gupta (@shubham107)*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vl;      typedef vector<int> vi;
typedef vector<vl> vvl;     typedef vector<vi> vvi;
typedef pair<ll, ll> pll;   typedef pair<int, int> pii;
#define forn(i,n) for(int i=0;i<n;i++)
#define rforn(i,n) for(int i=n-1;i>=0;i--)
#define forne(i,n) for(int i=1;i<=n;i++)
#define forse(i,s,e) for(int i=s;i<e;i++)
#define rforse(i,s,e) for(int i=e-1;i>=s;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define fill(a,x) memset(a,x,sizeof a);
#define trav(a,x) for(auto &a:x)
#define INF ll(1e18)
#ifdef SHUBHAM107
#include "../trace.h"
#else
#define trace(args...)
#endif
/*
    Use only when the modular is fixed;
    Computing inverse takes logN time so see whether to store it in a array;
*/
template <int mod = MOD>
struct Modular {
    int value;
    Modular(long long v = 0) {if(v >= MOD || v < -MOD) v %= MOD;value = v; if (value < 0) value += mod;}
    Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

    Modular& operator+=(Modular const& b) {value += b.value; if (value >= mod) value -= mod; return *this;}
    Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += mod;return *this;}
    Modular& operator*=(Modular const& b) {long long res = (long long)value * b.value;if(res >= MOD){res %= MOD;}value = res;return *this;}

    friend Modular mexp(Modular a, long long e) {
        e %= (mod - 1);
        Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
        return res;
    }
    friend Modular inverse(Modular a) { return mexp(a, mod - 2); }

    Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
    friend Modular operator+(Modular a, Modular const b) { return a += b; }
    friend Modular operator-(Modular a, Modular const b) { return a -= b; }
    friend Modular operator-(Modular const a) { return 0 - a; }
    friend Modular operator*(Modular a, Modular const b) { return a *= b; }
    friend Modular operator/(Modular a, Modular const b) { return a /= b; }
    friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
    friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
    friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

int mul(int a, int b) {long long res = a;res *= b;if (res >= MOD)return res % MOD;return res;}
int modpow(ll a, ll b) {a %= MOD; int res = 1; while (b) {if (b & 1)res = mul(res, a); a = mul(a, a); b >>= 1;} return res;}
int modinv(ll a) {return modpow(a, MOD - 2);}

using Mint = Modular<>;
const int mxN = 5e5;
ll fac[mxN+5], inv[mxN+5];
void pre(){
    fac[0] = 1;
    forne(i, mxN) fac[i] = (fac[i-1] * i) % MOD;
    inv[mxN] = modinv(fac[mxN]);
    rforn(i, mxN) inv[i] = (inv[i+1] * (i + 1)) % MOD;
    assert(inv[0] == 1 && inv[1] == 1);
}
ll ncr(ll n,ll r){
    if(r < 0 || r > n) return 0;
    ll ans = fac[n];
    ans = ans * inv[n-r] % MOD;
    ans = ans * inv[r] % MOD;
    return ans;
}
void solve() {
    int n, m;
    cin >> n >> m;
    Mint ans = 0;
    trace(ncr(0, 0));
    for(int i = 0; i <= n; i++) {
        int mul = i & 1 ? -1 : 1;
        Mint temp = mul * Mint(ncr(n, i)) * Mint(ncr(m, i)) * Mint(fac[i]) * mexp(fac[n - i] * Mint(ncr(m - i, n - i)), 2);
        ans += temp;
        // trace(Mint(fac[i]), temp);
    }
    cout << ans;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifdef SHUBHAM107
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("err.txt", "w", stderr);
#endif
    int t = 1;
    pre();
    // cin >> t;
    while (t--) {
        solve();
    }
}


Measured memory_bytes: 11412