Problem:
Score : 500 points

Problem Statement

Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions:

A_i \neq B_i, for every i such that 1\leq i\leq N.

A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N.

Since the count can be enormous, print it modulo (10^9+7).

Constraints

1\leq N \leq M \leq 5\times10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count modulo (10^9+7).

Sample Input 1

2 2

Sample Output 1

2

A_1=1,A_2=2,B_1=2,B_2=1 and A_1=2,A_2=1,B_1=1,B_2=2 satisfy the conditions.

Sample Input 2

2 3

Sample Output 2

18

Sample Input 3

141421 356237

Sample Output 3

881613484

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]

use proconio::{fastout, input};
use std::cmp::{max, min};

const INF: i32 = 1001001001;
const LNF: i64 = 1001001001001001001;
const MOD: u32 = 1000000007;

#[fastout]
fn main() {
  input! {
    n: usize,
    m: usize,
  }
  let mc = ModCalc::new(500000);
  let all = (mc.facc[m] / mc.facc[m - n]).pow(2);
  let mut acc = ModInt(0);
  for i in 1..(n + 1) {
    let a = mc.facc[m] / mc.facc[m - i];
    let b = (mc.facc[m - i] / mc.facc[m - n]).pow(2);
    if i % 2 == 1 {
      acc = acc + mc.com(n, i) * a * b;
    } else {
      acc = acc - mc.com(n, i) * a * b;
    }
  }
  let ans = all - acc;
  println!("{}", ans);
}

trait Monoid {
  fn one() -> Self;
}

trait Powerable: std::ops::Mul<Output = Self> + Monoid + Copy {
  fn pow(self, mut n: u64) -> Self {
    let mut r = Self::one();
    let mut x = self;
    while n > 0 { if n & 1 == 1 { r = r * x; } x = x * x; n >>= 1; }
    r
  }
}

#[derive(Clone, Copy)]
struct ModInt(u32);

impl Monoid for ModInt {
  fn one() -> Self { Self(1) }
}

impl Powerable for ModInt {}

impl std::fmt::Display for ModInt {
  fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
    write!(f, "{}", self.0)
  }
}

impl std::ops::Add for ModInt {
  type Output = Self;
  fn add(self, rhs: Self) -> Self {
    let x = self.0 + rhs.0;
    Self(if x >= MOD { x - MOD } else { x })
  }
}

impl std::ops::Sub for ModInt {
  type Output = Self;
  fn sub(self, rhs: Self) -> Self {
    let x = self.0 + MOD - rhs.0;
    Self(if x >= MOD { x - MOD } else { x })
  }
}

impl std::ops::Mul for ModInt {
  type Output = Self;
  fn mul(self, rhs: Self) -> Self {
    let x = self.0 as u64 * rhs.0 as u64;
    Self(if x >= MOD as u64 { x % MOD as u64 } else { x } as u32)
  }
}

impl std::ops::Div for ModInt {
  type Output = Self;
  fn div(self, rhs: Self) -> Self {
    self * rhs.inv()
  }
}

impl ModInt {
  fn new(x: u32) -> Self {
    Self(x % MOD)
  }
  fn inv(self) -> Self {
    self.pow((MOD - 2) as u64)
  }
}

struct ModCalc {
  facc: Vec<ModInt>,
  finv: Vec<ModInt>,
}

impl ModCalc {
  fn new(n: usize) -> Self {
    let mut facc = vec![ModInt(0); n + 1];
    let mut finv = vec![ModInt(0); n + 1];
    facc[0] = ModInt::one();
    for i in 0..n {
      facc[i + 1] = facc[i] * ModInt((i + 1) as u32);
    }
    finv[n] = ModInt::one() / facc[n];
    for i in (0..n).rev() {
      finv[i] = finv[i + 1] * ModInt((i + 1) as u32);
    }
    ModCalc { facc, finv }
  }
  fn com(&self, a: usize, b: usize) -> ModInt {
    if b > a {
      return ModInt(0);
    }
    self.facc[a] * self.finv[a - b] * self.finv[b]
  }
}