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


//use std::collections::HashMap;


const PRIME: i64 = 1_000_000_007;

fn main(){
    let arg: Vec<i64> = read_vec();
    let n = arg[0];
    let m = arg[1];
    let mut fact = vec![1i64; 510000];
    let mut factinv = vec![1i64; 510000];

    for i in 1..=500000 {
        fact[i] = fact[i-1] * i as i64 %PRIME;
        factinv[i] = modpow(fact[i], PRIME-2);
    }
    factinv[500000] = modpow(fact[500000], PRIME-2);
    for i in (1..=500000).rev() {
        factinv[i-1] = factinv[i] * i as i64 %PRIME;
    }
    
    let comb = |n: i64, r: i64| {
        fact[n as usize]*factinv[r as usize] %PRIME *factinv[(n-r) as usize] %PRIME
    };
    let perm = |n: i64, r: i64| {
        fact[n as usize]*factinv[(n-r) as usize] %PRIME
    };

    let mut ans = 0i64;
    for k in 0..=n {
        let t1 = comb(n, k)*perm(m, k) %PRIME;
        let t2 = perm(m-k, n-k).pow(2) %PRIME;
        let t3 = (-1i64).pow(k as u32);
        ans += t1*t2*t3;
        ans = ((ans%PRIME)+PRIME)%PRIME
    }

    println!("{}", ans);
}

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

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


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

fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}