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)

struct Combination {
    fact: Vec<usize>,
    inv_fact: Vec<usize>,
}

impl Combination {
    const MOD: usize = 1_000_000_007;

    fn new(n: usize) -> Combination {
        let mut fact = vec![0; n + 1];
        let mut inv_fact = vec![0; n + 1];
        fact[0] = 1;
        for i in 1..=n {
            fact[i] = fact[i - 1] * i % Self::MOD;
        }
        inv_fact[n] = Self::pow(fact[n], Self::MOD - 2);
        for i in (1..=n).rev() {
            inv_fact[i - 1] = inv_fact[i] * i % Self::MOD;
        }

        Combination { fact, inv_fact }
    }

    fn pow(mut a: usize, mut n: usize) -> usize {
        let mut res = 1;
        while n > 0 {
            if n & 1 > 0 {
                res = res * a % Self::MOD;
            }
            a = a * a % Self::MOD;
            n >>= 1;
        }
        res
    }

    fn p(&self, n: usize, r: usize) -> usize {
        self.fact[n] * self.inv_fact[n - r] % Self::MOD
    }
    
    fn c(&self, n: usize, r: usize) -> usize {
        if n < r { return 0; }
        let tmp = self.inv_fact[n - r] * self.inv_fact[r] % Self::MOD;
        self.fact[n] * tmp % Self::MOD
    }
}

fn main() {
    const MOD: usize = 1_000_000_007;

    let (n, m): (usize, usize) = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        (
            iter.next().unwrap().parse().unwrap(),
            iter.next().unwrap().parse().unwrap(),
        )
    };

    let comb = Combination::new(m);
    let mut res = comb.p(m, n);
    for i in 1..=n {
        if i % 2 == 1 {
            res += MOD;
            res -= comb.p(m - i, n - i) * comb.c(n, i) % MOD;
            res %= MOD;
        } else {
            res += comb.p(m - i, n - i) * comb.c(n, i) % MOD;
            res %= MOD;
        }
    }
    let ans = res * comb.p(m, n) % MOD;
    println!("{}", ans);
}