func pow(_ x: Int, _ n: Int, mod: Int) -> Int {
    precondition(n >= 0 && mod > 1)
    if n == 0 { return 1 }

    var u = pow(x, n/2, mod: mod)
    u = u*u % mod
    if n%2 == 1 { u = u*x % mod }
    return u
}

//=============================================================================

let MOD = Int(1e9+7)

let line = readLine()!.split(separator: " ").map { Int($0)! }
let (N, M) = (line[0], line[1])

var modFactorial = [1]
for i in 1...M {
    modFactorial.append(modFactorial[i-1] * i % MOD)
}

func modinv(_ x: Int) -> Int {
    pow(x, MOD-2, mod: MOD)
}
var modinvCache = [Int]()
for i in 0...M {
    modinvCache.append(modinv(modFactorial[i]))
}

func modperm(_ n: Int, _ k: Int) -> Int {
    modFactorial[n] * modinvCache[n-k] % MOD
}

func modcomb(_ n: Int, _ k: Int) -> Int {
    modperm(n, k) * modinvCache[k] % MOD
}

var sigma = 0
for i in 0...N {
    var x = 1
    x *= modcomb(N, i)
    x %= MOD
    x *= modperm(M, i)
    x %= MOD
    x *= modperm(M-i, N-i)
    x %= MOD
    x *= modperm(M-i, N-i)
    x %= MOD
    x *= i%2 == 0 ? 1 : -1
    sigma += x
}
sigma = sigma % MOD + (sigma < 0 ? MOD : 0)
print(sigma)
