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



fn main(){
    const MOD: i64 = 1_000_000_007;
    let arg: Vec<usize> = input_vec();
    let n = arg[0] as i64;
    let m = arg[1] as i64;

    let md = Mod::new(MOD, 500000);
    
    let mut ans = 0;
    
    for i in 0..=n {
        let i = i as i64;
        let t = md.comb(n, i) * md.perm(m, i) %MOD;
        let t = t * md.pow(md.perm(m-i, n-i), 2) %MOD;
        let t = t * md.pow(-1, i);
        ans += t;
        ans = (ans%MOD+MOD)%MOD;
    }
    println!("{}", ans);
}

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

mod Mod {
    pub struct Elements {
        memo_fact: Vec<i64>,
        memo_invfact: Vec<i64>,
        MOD: i64,
    }

    pub fn new(n: i64, max_fact_size: i64) -> Elements {
        Elements::new(n, max_fact_size)
    }

    fn _pow(mut r: i64, mut n: i64, MOD: i64) -> i64 {
        let mut res = 1i64;
        while n>0 {
            if n%2==1 { res = res*r %MOD; }
            r = r*r % MOD;
            n /= 2;
        }
        res
    }
    
    impl Elements{
        pub fn new(n: i64, max_fact_size: i64) -> Elements {
            let MOD = n;
            let max_fact_size = max_fact_size as usize + 10;
            let mut memo_fact = vec![1i64; max_fact_size];
            let mut memo_invfact = vec![1i64; max_fact_size];
            
            for i in 2..max_fact_size {
                memo_fact[i] = memo_fact[i-1] * i as i64 %MOD;
            }
            memo_invfact[max_fact_size-1] = _pow(memo_fact[max_fact_size-1], MOD-2, MOD);
            for i in (2..max_fact_size).rev() {
                memo_invfact[i-1] = memo_invfact[i] * i as i64 %MOD;
            }
            
            Elements {memo_fact, memo_invfact, MOD}
        }

        pub fn pow(&self, r: i64, n: i64) -> i64 {
            _pow(r, n, self.MOD)
        }

        pub fn inv(&self, n: i64) -> i64 {
            self.pow(n, self.MOD-2)
        }

        pub fn fact(&self, n: i64) -> i64 {
            self.memo_fact[n as usize]
        }

        pub fn invfact(&self, n: i64) -> i64 {
            self.memo_invfact[n as usize]
        }

        pub fn perm(&self, n: i64, r: i64) -> i64 {
            self.fact(n)*self.invfact(n-r) %self.MOD
        }
    
        pub fn comb(&self, n: i64, r: i64) -> i64 {
            self.perm(n, r) * self.invfact(r) %self.MOD
        }
    }

}



/*
 * 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()
}