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: Runtime Error)

#![allow(unused_imports)]
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::collections::BinaryHeap;

#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_end().to_owned()
}

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}
impl<T: Iterator> IteratorExt for T {}

fn main() {
    let (n, m, mut k) = read!(usize, usize, usize);
    let a = read!([usize]);
    let b = read!([usize]);

    let mut idx_a = 0;
    let mut idx_b = 0;

    let mut count = 0;
    loop {
        if idx_a >= n && idx_b >= m {
            break;
        } else if idx_a >= n {
            if k >= b[idx_b] {
                k -= b[idx_b];
                count += 1;
                idx_b += 1;
            } else {
                break;
            }
        } else if idx_b >= m {
            if k >= a[idx_a] {
                k -= a[idx_a];
                count += 1;
                idx_a += 1;
            } else {
                break;
            }
        } else {
            if a[idx_a] < b[idx_b] {
                if k >= a[idx_a] {
                    k -= a[idx_a];
                    count += 1;
                    idx_a += 1;
                } else {
                    break;
                }
            } else {
                if k >= b[idx_b] {
                    k -= b[idx_b];
                    count += 1;
                    idx_b += 1;
                } else {
                    break;
                }
            }
        }
    }

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