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)

use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn inv(a: usize) -> usize {
    let m = M as i64;
    let mut a = a as i64;
    let mut b = m as i64;
    let mut u = 1;
    let mut v = 0;
    let mut tmp;
    while b != 0 {
        let t = a / b;
        a -= t * b;
        tmp = a;
        a = b;
        b = tmp;
        u -= t * v;
        tmp = u;
        u = v;
        v = tmp;
    }
    u %= m;
    if u < 0 {
        u += m;
    }
    return u as usize;
}

fn main() {
    input! {
        n: usize,
        m: usize,
    }
    let mut f = vec![1; m + 1];
    let mut g = vec![1; m + 1];
    for i in 1..=m {
        f[i] = i * f[i - 1] % M;
        g[i] = inv(f[i]);
    }
    // let mut h1 = vec![1; n + 1];
    // let mut h2 = vec![1; n + 1];
    // for k in 1..=n {
    //     let s = (m - n + k) * inv(k) % M;
    //     h1[k] = s * h1[k - 1] % M;
    //     eprintln!("{}", (s + M - h1[k]));
    // }
    // eprintln!("{:?}", f);
    // eprintln!("{:?}", g);
    // eprintln!("{:?}", h1);
    let mut result = 0;
    for k in 0..=n {
        let s = f[m - k] * g[m - n] % M;
        let s = s * s % M;
        let t = f[n] * g[k] % M * g[n - k] % M;
        let u = f[m] * g[m - k] % M;
        // eprintln!("{} {} {}", s, t, u);
        if k % 2 == 0 {
            result = (result + s * t % M * u % M) % M;
        } else {
            result = (result + M - s * t % M * u % M) % M;
        }
    }
    println!("{}", result);
}