fun main() {
  val (n, m) = readLine()!!.split(' ').map { it.toInt() }
  val fact = Array(m + 1) { ModN(it) }
  fact[0] = ModN(1)
  for (i in 3 .. m) {
    fact[i] *= fact[i - 1]
  }
  val invFact = Array(m + 1) { fact[it].inv() }
  fun a(n: Int, k: Int): ModN {
    return fact[n] * invFact[n - k]
  }
  fun c(n: Int, k: Int): ModN {
    return fact[n] * invFact[k] * invFact[n - k]
  }
  var answer = a(m, n).sqr()
  for (i in 1 .. n) {
    val cur = c(m, i) * c(n, i) * fact[i] * a(m - i, n - i).sqr()
    if (i % 2 == 0) answer += cur
    else answer -= cur
  }
  println(answer)
}

const val MODULO = 1000_000_000 + 7

private class ModN(private val x: Int) {
  operator fun plus(other: ModN) = ModN((this.x + other.x) % MODULO)
  operator fun times(other: ModN) = ModN(((this.x.toLong() * other.x) % MODULO).toInt())
  operator fun minus(other: ModN) = this + -other
  operator fun unaryMinus(): ModN = ModN(MODULO - x)
  override fun toString() = x.toString()
}

private fun ModN.pow(p: Int): ModN {
  var pow = p
  var res = ModN(1)
  var mul = this
  while (pow > 0) {
    if (pow % 2 == 1) {
      res *= mul
    }
    mul *= mul
    pow = pow ushr 1
  }
  return res
}

private fun ModN.inv(): ModN = this.pow(MODULO - 2)

private fun ModN.sqr(): ModN = this * this

