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)]

const MOD: i64 = 1_000_000_007;

fn main(){
    let arg: Vec<usize> = input_vec();
    let n = arg[0] as i64;
    let m = arg[1] as i64;
    
    let math = Math::new(m);
    let mut ans = 0;
    
    for i in 0..=n {
        let i = i as i64;
        let t1 = math.comb(n, i) * math.perm(m, i) %MOD;
        let t2 = modpow(math.perm(m-i, n-i), 2);
        let t3 = modpow(-1, i);
        let t = t1*t2%MOD * t3;
        ans += t;
        ans = (ans%MOD+MOD)%MOD;
    }
    println!("{}", ans);
}

struct Math {
    fact:Vec<i64>,
    factinv:Vec<i64>,
}

impl Math {
    fn new(n: i64) -> Math {
        let n = n as usize + 1;
        let mut fact = vec![1i64; n];
        let mut factinv = vec![1i64; n];
        
        for i in 2..n {
            fact[i] = fact[i-1] * i as i64 %MOD;
        }
        factinv[n-1] = modinv(fact[n-1]);
        for i in (2..n).rev() {
            factinv[i-1] = factinv[i] * i as i64 %MOD;
        }
        Math {fact, factinv}
    }

    fn perm(&self, n: i64, r: i64) -> i64 {
        let n = n as usize;
        let r = r as usize;
        self.fact[n] * self.factinv[n-r] %MOD
    }

    fn comb(&self, n: i64, r: i64) -> i64 {
        self.perm(n, r) * self.factinv[r as usize] %MOD
    }
}


/*
 * modpow
 */
fn modpow(mut r: i64, mut n: i64) -> i64{
    let mut res = 1;
    while n>0 {
        if n%2 == 1 {res = res*r%MOD;}
        r = r*r%MOD;
        n = n/2;
    }
    res
}

fn modinv(n: i64) -> i64{
    modpow(n, MOD-2)
}


/*
 * input function
 */
fn input<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}


fn input_vec<T: std::str::FromStr>() -> Vec<T> {
    input::<String>()
        .split_whitespace()
        .map(|s| s.parse().ok().unwrap()).collect()
}