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 PyPy3 (7.3.0) (judge verdict: Time Limit Exceeded)

import sys
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

import functools
@functools.lru_cache(maxsize=None)
def combi(n, r):
  r = min(r, n-r)
  numer = denom = 1
  for i in range(1, r+1):
    numer = numer * (n+1-i) %mod
    denom = denom * i %mod
  return numer * pow(denom, mod-2, mod) %mod

def tami(n, r):
  if r == 0:
    res = kaizyou[n]
    return res %mod
  elif r == 1:
    res = kaizyou[n-1] * (n-1)
    return res %mod
  else:
    return (tami(n-1, r-1) + tami(n-1, r-2) )%mod

N,M = IL()

kaizyou = [0,1]
for i in range(2,N+1):
  kaizyou.append(kaizyou[-1]*i %mod)

ans = 0
for i in range(max(0, 2*N-M), N+1):
  ans += ((combi(N, i) * combi(M-N, N-i) ) %mod * tami(N, i)) %mod
print((((combi(M, N) * kaizyou[N]) %mod) * ans) %mod)