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)

// -*- coding:utf-8-unix -*-

use proconio::input;

use num_traits::{pow, One};
use std::ops::{Add, Div, Mul, Sub};

const MODULUS: usize = 1000000007;

// ABC086C - Traveling
// https://atcoder.jp/contests/abs/fasks/arc089_a

fn main() {
    input! {
        n: usize,
        m: usize,
    }

    let mm = ModP(m);
    let nn = ModP(n);

    // prepare a table of factorials
    let mut fac_vec: Vec<ModP> = Vec::new();
    let mut fac_inv_vec: Vec<ModP> = Vec::new();
    fac_vec.push(ModP(1));
    fac_inv_vec.push(ModP(1));
    for i in 1..=m {
        fac_vec.push(ModP(i) * fac_vec[i - 1]);
        fac_inv_vec.push(ModP(1) / fac_vec[i]);
    }
    let fac_vec = fac_vec;
    let fac_inv_vec = fac_inv_vec;

    let mut result = ModP(0);
    for i in 0..=n {
        if i % 2 == 0 {
            result = result + num_of_pair_of_seq_with_i_same_entries(mm, nn, ModP(i), &fac_vec, &fac_inv_vec);
        } else {
            result = result - num_of_pair_of_seq_with_i_same_entries(mm, nn, ModP(i), &fac_vec, &fac_inv_vec);
        }
    }
    println!("{}", result.0);
}

fn permutation(n: ModP, r: ModP, fac_vec: &Vec<ModP>, fac_inv_vec: &Vec<ModP>) -> ModP {
    return fac_vec[n.0] * fac_inv_vec[(n - r).0];
}

fn combination(n: ModP, r: ModP, fac_vec: &Vec<ModP>, fac_inv_vec: &Vec<ModP>) -> ModP {
    return fac_vec[n.0] * fac_inv_vec[(n - r).0] * fac_inv_vec[r.0];
}

fn num_of_pair_of_seq_with_i_same_entries(
    m: ModP,
    n: ModP,
    i: ModP,
    fac_vec: &Vec<ModP>,
    fac_inv_vec: &Vec<ModP>,
) -> ModP {
    let comb = combination(n, i, fac_vec, fac_inv_vec);
    let perm1 = permutation(m, i, fac_vec, fac_inv_vec);
    let perm2 = permutation(m-i, n-i, fac_vec, fac_inv_vec);
    return comb * perm1 * perm2 * perm2;
}

#[derive(Clone, Copy, PartialEq, Debug)]
struct ModP(usize);

impl One for ModP {
    fn one() -> Self {
        return ModP(1);
    }
}
impl Add for ModP {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        return ModP((self.0 + rhs.0) % MODULUS);
    }
}
impl Sub for ModP {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        return ModP((self.0 + MODULUS - rhs.0) % MODULUS);
    }
}
impl Mul for ModP {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        return ModP((self.0 * rhs.0) % MODULUS);
    }
}
impl Div for ModP {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        if rhs.0 == 0 {
            panic!("Tried to divide by ModP(0)!");
        }
        let rhs_inv = pow(rhs, MODULUS - 2);
        return self * rhs_inv;
    }
}