#include<bits/stdc++.h>

#define int long long
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;

constexpr int mod = 1000000007;
#define ll long long

template<ll M = mod>
class ModInt {
public:
    ModInt(ll data = 0) : data((data % M + M) % M) {}

    inline ModInt<M> &operator+=(ModInt<M> const &right) {
        data += right.data;
        data = data >= M ? data - M : data;
        return *this;
    }

    inline ModInt<M> &operator-=(ModInt<M> const &right) {
        data += M - right.data;
        return *this;
    }

    inline ModInt<M> &operator*=(ModInt<M> const &right) {
        data = data * right.data % M;
        data = data < 0 ? data + M : data;
        return *this;
    }

    inline ModInt<M> &operator/=(ModInt<M> const &right) {
        *this *= inv(right);
        return *this;
    }

    inline bool operator==(ModInt<M> const &right) const {
        return data == right.data;
    }

    inline bool operator!=(ModInt<M> const &right) const {
        return !(*this == right);
    }

    inline bool operator<(ModInt<M> const &right) const {
        return data < right.data;
    }

    inline ll value() const {
        return data;
    }

    //k!
    inline static ModInt<M> Fact(int k) {
        if (k < 0)return 0;
        if (fact.size() >= k + 1)return fact[k];

        int calculated = fact.size();//計算済み
        fact.resize(k + 1);
        fact_inv.resize(k + 1);
        if (calculated == 0) {
            fact[0] = fact_inv[0] = 1;
            calculated = 1;
        }
        for (int i = calculated; i <= k; ++i) {
            fact[i] = fact[i - 1] * i;
        }
        fact_inv[k] = inv(fact[k]);
        for (int i = k - 1; i >= calculated; --i) {
            fact_inv[i] = fact_inv[i + 1] * (i + 1);
        }
        return fact[k];
    }

    //1/k!
    inline static ModInt<M> Fact_inv(int k) {
        if (k < 0)return 0;
        Fact(k);
        return fact_inv[k];
    }

private:
    ll data;
    static vector<ModInt<M>> fact;
    static vector<ModInt<M>> fact_inv;
};

template<ll M> vector<ModInt<M>> ModInt<M>::fact = vector<ModInt<M>>();
template<ll M> vector<ModInt<M>> ModInt<M>::fact_inv = vector<ModInt<M>>();

template<ll M>
inline ModInt<M> operator+(const ModInt<M> &left, const ModInt<M> &right) {
    return ModInt<M>(left) += right;
}

template<ll M>
inline ModInt<M> operator+(int left, const ModInt<M> &right) {
    return ModInt<M>(left) + right;
}

template<ll M>
inline ModInt<M> operator+(const ModInt<M> &left, int right) {
    return left + ModInt<M>(right);
}

template<ll M>
inline ModInt<M> operator-(const ModInt<M> &left, const ModInt<M> &right) {
    return ModInt<M>(left) -= right;
}

template<ll M>
inline ModInt<M> operator-(int left, const ModInt<M> &right) {
    return ModInt<M>(left) - right;
}

template<ll M>
inline ModInt<M> operator-(const ModInt<M> &left, int right) {
    return left - ModInt<M>(right);
}

template<ll M>
inline ModInt<M> operator*(const ModInt<M> &left, const ModInt<M> &right) {
    return ModInt<M>(left) *= right;
}

template<ll M>
inline ModInt<M> operator*(int left, const ModInt<M> &right) {
    return ModInt<M>(left) * right;
}

template<ll M>
inline ModInt<M> operator*(const ModInt<M> &left, int right) {
    return left * ModInt<M>(right);
}

template<ll M>
inline ModInt<M> operator/(const ModInt<M> &left, const ModInt<M> &right) {
    return ModInt<M>(left) /= right;
}

template<ll M>
inline ModInt<M> operator/(int left, const ModInt<M> &right) {
    return ModInt<M>(left) / right;
}

template<ll M>
inline ModInt<M> operator/(const ModInt<M> &left, int right) {
    return left / ModInt<M>(right);
}

template<ll M>
inline ostream &operator<<(ostream &os, const ModInt<M> mi) {
    os << mi.value();
    return os;
}

template<ll M>
inline ModInt<M> pow(ModInt<M> a, ll b) {
    ModInt<M> res = 1;
    while (b) {
        if (b & 1)res *= a;
        b >>= 1;
        a *= a;
    }
    return res;
}

template<ll M>
inline ModInt<M> inv(ModInt<M> a) {
    return pow(a, M - 2);
}

template<ll M>
inline ModInt<M> comb(int n, int r) {
    return ModInt<mod>::Fact(n) * ModInt<mod>::Fact_inv(r) * ModInt<mod>::Fact_inv(n - r);
}

template<ll M>
inline ModInt<M> perm(int n, int r) {
    return ModInt<mod>::Fact(n) * ModInt<mod>::Fact_inv(n - r);
}

signed main() {
    int N, M;
    cin >> N >> M;
    ModInt<mod> ans = pow(perm<mod>(M, N), 2);
    for (int k = 1; k <= N; ++k) {
        ans += (k % 2 ? -1 : 1) * comb<mod>(N, k) * perm<mod>(M, k) * pow(perm<mod>(M - k, N - k), 2);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 14828