AtCoder Beginner Contest 172 - NEQ

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-----
2 2

-----Sample Output-----
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.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
(n, m) = map(int, input().split())
(M, F) = (10 ** 9 + 7, [1])
for i in range(m):
	F += [-~i * F[i] % M]
print(sum(((-1) ** k * F[m - k] * pow(F[k] * F[n - k] * F[m - n] ** 2, -1, M) for k in range(n + 1))) * F[n] * F[m] % M)


(N, M) = map(int, input().split())
MAX = 5 * 10 ** 5 + 1
MOD = 10 ** 9 + 7
fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1
for i in range(2, MAX + 1):
	fac[i] = fac[i - 1] * i % MOD
finv = [0] * (MAX + 1)
finv[MAX] = pow(fac[MAX], MOD - 2, MOD)
for i in reversed(range(1, MAX + 1)):
	finv[i - 1] = finv[i] * i % MOD

def comb(n, k):
	if n < k or k < 0:
		return 0
	return fac[n] * finv[k] * finv[n - k] % MOD

def permute(n, k):
	return comb(n, k) * fac[k]
ans = 0
for k in range(N + 1):
	ans += comb(N, k) * (-1) ** k * permute(M, k) * permute(M - k, N - k) ** 2
	ans %= MOD
print(ans)


(n, m) = list(map(int, input().split()))
(a, M, d) = (1, 10 ** 9 + 7, [1] * (n + 1))
for i in range(n):
	d[i + 1] = ((m - n + i) * d[i] + i * d[i - 1]) % M
	a = a * (m - i) % M
print(a * d[-1] % M)


(N, M) = map(int, input().split())
ans = 0
mod = 10 ** 9 + 7

def modinv(a, mod=10 ** 9 + 7):
	return pow(a, mod - 2, mod)
fac = [0] * (M + 1)
fac[0] = 1
fac[1] = 1
for i in range(2, M + 1):
	fac[i] = fac[i - 1] * i % mod

def combi(n, r, mod=10 ** 9 + 7):
	return fac[n] * modinv(fac[r]) % mod * modinv(fac[n - r]) % mod
for k in range(N + 1):
	ans += fac[M - k] * modinv(fac[M - N]) % mod * combi(N, k) * (-1) ** k
print(ans * fac[M] * modinv(fac[M - N]) % mod)


(n, m) = map(int, input().split())
i = b = 0
a = 1
while i < n:
	(a, b) = ((m - i) * ((m - n + i) * a + i * (m - i + 1) * b) % (10 ** 9 + 7), a)
	i += 1
print(a)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
fact = [1] * (m + 1)
inv = [1] * (m + 1)
for i in range(2, m + 1):
	fact[i] = i * fact[i - 1] % mod
inv[-1] = pow(fact[-1], mod - 2, mod)
for i in range(m, 1, -1):
	inv[i - 1] = inv[i] * i % mod

def comb(x, y):
	return fact[x] * inv[y] % mod * inv[x - y] % mod if x >= y >= 0 else 0

def p(x, y):
	return fact[x] * inv[x - y] % mod if x >= y >= 0 else 0
ans = p(m, n) ** 2 % mod
a = -1
for i in range(1, n + 1):
	ans = (ans + a * (p(m, i) * comb(n, i) * p(m - i, n - i) ** 2 % mod)) % mod
	a *= -1
print(ans)


import sys
if sys.argv[-1] == 'ONLINE_JUDGE':
	import os
	import re
	with open(__file__) as f:
		source = f.read().split('###nbacl')
	for s in source[1:]:
		s = re.sub("'''.*", '', s)
		sp = s.split(maxsplit=1)
		if os.path.dirname(sp[0]):
			os.makedirs(os.path.dirname(sp[0]), exist_ok=True)
		with open(sp[0], 'w') as f:
			f.write(sp[1])
	from nbmodule import cc
	cc.compile()
from nbmodule import solve
(N, M) = [int(x) for x in input().split()]
ans = solve(N, M)
print(ans)
__starting_point()


(N, M) = map(int, input().split())
md = 1000000007
fac = [0] * (M + 1)
inv = [0] * (M + 1)
ifac = [0] * (M + 1)
inv[1] = 1
for i in range(2, M + 1):
	inv[i] = (md - md // i * inv[md % i] % md) % md
fac[0] = 1
ifac[0] = 1
for i in range(1, M + 1):
	fac[i] = fac[i - 1] * i % md
	ifac[i] = ifac[i - 1] * inv[i] % md

def choose(n, k):
	return fac[n] * ifac[k] * ifac[n - k] % md

def perm(m, n):
	return fac[m] * ifac[m - n] % md
ans = 0
for k in range(N + 1):
	if k % 2 == 0:
		ans += choose(N, k) * perm(M, k) * perm(M - k, N - k) ** 2
		ans %= md
	else:
		ans -= choose(N, k) * perm(M, k) * perm(M - k, N - k) ** 2
		ans %= md
print(ans)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 + 7
(N, M) = map(int, readline().split())
fact = [1] * (M + 1)
fact_inv = [1] * (M + 1)
for i in range(1, M + 1):
	fact[i] = fact[i - 1] * i % mod
fact_inv[M] = pow(fact[M], mod - 2, mod)
for i in range(M, 0, -1):
	fact_inv[i - 1] = i * fact_inv[i] % mod

def comb(n, r):
	return fact[n] * fact_inv[r] * fact_inv[n - r] % mod
total = 0
for k in range(N + 1):
	a = fact[M - k] * fact_inv[M - N] * comb(N, k) % mod
	if k % 2 == 0:
		total += a
	else:
		total -= a
ans = comb(M, N) * fact[N] % mod * total
print(ans % mod)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
MAXN = N + 5
fac = [1, 1] + [0] * MAXN
finv = [1, 1] + [0] * MAXN
inv = [0, 1] + [0] * MAXN
for i in range(2, MAXN + 2):
	fac[i] = fac[i - 1] * i % MOD
	inv[i] = -inv[MOD % i] * (MOD // i) % MOD
	finv[i] = finv[i - 1] * inv[i] % MOD

def comb(n, r):
	if n < r:
		return 0
	if n < 0 or r < 0:
		return 0
	return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD
ans = 1
for i in range(N):
	ans *= M - i
	ans %= MOD
ps = [1]
for i in range(N):
	ps.append(ps[-1] * (M - N + 1 + i) % MOD)
ps.reverse()
tmp = 0
for (i, p) in enumerate(ps):
	t = p * comb(N, i)
	if i % 2:
		t *= -1
	tmp += t
ans *= tmp
ans %= MOD
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
nums = 5 * 10 ** 5
(g1, g2, inverse) = ([1, 1], [1, 1], [0, 1])
for num in range(2, nums + 1):
	g1.append(g1[-1] * num % mod)
	inverse.append(-inverse[mod % num] * (mod // num) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)

def nPr(n, r):
	return g1[n] * g2[n - r] % mod

def nCr(n, r):
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
ans = 0
for r in range(n + 1):
	tmp = nCr(n, r) * nPr(m, r) * nPr(m - r, n - r) ** 2 % mod
	ans = (ans + (-1) ** r * tmp) % mod
print(ans)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())

def invmod(a):
	return pow(a, MOD - 2, MOD)
fact_list = [1]
fact_inv_list = [1]
for i in range(1, M + 1):
	fact_mod = fact_list[-1] * i % MOD
	fact_list.append(fact_mod)
	fact_inv_list.append(invmod(fact_mod))

def comb_mod_table(n, r):
	if 0 <= r <= n:
		return fact_list[n] * fact_inv_list[r] * fact_inv_list[n - r]
	else:
		return 0

def perm_mod_table(n, r):
	if 0 <= r <= n:
		return fact_list[n] * fact_inv_list[n - r]
	else:
		return 0
answer = 0
for i in range(N + 1):
	term = comb_mod_table(N, i) * pow(-1, i) * perm_mod_table(M, i) * perm_mod_table(M - i, N - i) * perm_mod_table(M - i, N - i)
	answer = (answer + term) % MOD
print(answer)


import sys, queue, math, copy, itertools, bisect, collections, heapq

def main():
	MOD = 10 ** 9 + 7
	LI = lambda : [int(x) for x in sys.stdin.readline().split()]
	(N, M) = LI()
	f = [1]
	r = [1]
	c = 1
	for i in range(1, M + 1):
		c = c * i % MOD
		f.append(c)
		r.append(pow(c, MOD - 2, MOD))

	def comb(n, k):
		return f[n] * r[k] * r[n - k] % MOD

	def perm(n, k):
		return f[n] * r[n - k] % MOD
	ans = 0
	for i in range(N + 1):
		ans = (ans + (-1) ** i * comb(N, i) * perm(M, i) * perm(M - i, N - i) ** 2) % MOD
	print(ans)
main()


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list('abcdefghijklmnopqrstuvwxyz')
ALF = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
INF = float('inf')
(N, M) = list(map(int, input().split()))
fac = [1, 1]
inv = [0, 1]
finv = [1, 1]
MOD = 10 ** 9 + 7
fac = [1, 1]
inv = [0, 1]
finv = [1, 1]
for i in range(2, M + 1):
	fac.append(fac[-1] * i % MOD)
	inv.append(MOD - inv[MOD % i] * (MOD // i) % MOD)
	finv.append(finv[-1] * inv[-1] % MOD)

def comb_mod(n, r, m):
	if n < 0 or r < 0 or n < r:
		return 0
	r = min(r, n - r)
	return fac[n] * finv[n - r] * finv[r] % m

def chofuku(n, r):
	return comb_mod(n + r - 1, r)

def nPr(n, r, m):
	return fac[n] * finv[n - r] % m
ans = 0
for i in range(1, N + 1):
	if i % 2 == 1:
		flag = 1
	else:
		flag = -1
	s = flag * comb_mod(N, i, MOD) * nPr(M - i, N - i, MOD)
	ans += s
	if ans > MOD:
		ans %= MOD
ans1 = 1
for i in range(N):
	ans1 *= M - i
	ans1 %= MOD
ans2 = ans1 * ans1 % MOD
ans3 = ans * nPr(M, N, MOD) % MOD
print((ans2 - ans3) % MOD)


mod = pow(10, 9) + 7
(n, m) = map(int, input().split())
ans = 0
inv = [0, 1]
fact = [1, 1]
ifact = [1, 1]
for i in range(2, m + 1):
	inv.append(inv[mod % i] * (mod - int(mod / i)) % mod)
	fact.append(i * fact[-1] % mod)
	ifact.append(ifact[-1] * inv[i] % mod)

def combi(a, b):
	return fact[a] * ifact[b] * ifact[a - b] % mod

def permi(a, b):
	return fact[a] * ifact[a - b] % mod
for i in range(n + 1):
	now = combi(n, i)
	now *= permi(m - i, n - i)
	now %= mod
	ans += now * (-1) ** (i & 1)
	ans %= mod
ans *= permi(m, n)
ans %= mod
print(ans)


MOD = 10 ** 9 + 7

def resolve():
	(N, M) = map(int, input().split())
	a = 1
	d = [1]
	for i in range(N):
		d.append(((M - N + i) * d[i] + i * d[i - 1]) % MOD)
		a = a * (M - i) % MOD
	print(a * d[-1] % MOD)
resolve()


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
leng = 500010
fac = [1, 1]
for i in range(2, 500010):
	fac.append(fac[-1] * i % mod)
invfac = [0] * 500010
invfac[500009] = pow(fac[500009], -1, mod)
for i in range(500009):
	invfac[500008 - i] = invfac[500009 - i] * (500009 - i) % mod

def combi(n, m):
	return fac[n] * invfac[m] * invfac[n - m] % mod

def permu(n, m):
	return fac[n] * invfac[n - m] % mod
Ans = 0
for i in range(N + 1):
	if i % 2 == 0:
		Ans += combi(N, i) * permu(M, i) * permu(M - i, N - i) ** 2
	else:
		Ans -= combi(N, i) * permu(M, i) * permu(M - i, N - i) ** 2
print(Ans % mod)


(n, m) = map(int, input().split())
dp = [0] * n
mod = 10 ** 9 + 7
if n == 1:
	print((m - n) * m % mod)
elif n == 2:
	ans = (m - n) ** 2 + (m - n + 1)
	for i in range(2):
		ans *= m - i
		ans %= mod
	print(ans)
else:
	dp[0] = m - n
	dp[1] = (m - n) ** 2 + (m - n + 1)
	for i in range(2, n):
		dp[i] = (dp[i - 1] + dp[i - 2]) * i + dp[i - 1] * (m - n)
		dp[i] %= mod
	ans = dp[-1]
	for i in range(n):
		ans *= m - i
		ans %= mod
	print(ans)


(n, m) = map(int, input().split())
N = m + 100
MOD = 10 ** 9 + 7
fact = [0 for _ in range(N)]
invfact = [0 for _ in range(N)]
fact[0] = 1
for i in range(1, N):
	fact[i] = i * fact[i - 1] % MOD
invfact[N - 1] = pow(fact[N - 1], MOD - 2, MOD)
for i in range(N - 2, -1, -1):
	invfact[i] = invfact[i + 1] * (i + 1) % MOD

def nCk(n, k):
	if k < 0 or n < k:
		return 0
	else:
		return fact[n] * invfact[k] * invfact[n - k] % MOD

def nPk(n, k):
	return fact[n] * invfact[n - k] % MOD
ans = 0
sign = 1
for i in range(n + 1):
	ans = (ans + nPk(m, i) * nCk(n, i) * nPk(m - i, n - i) ** 2 * sign) % MOD
	sign *= -1
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7

def comb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return fac[n] * (finv[r] * finv[n - r] % mod) % mod

def perm(n, r, mod):
	if r < 0 or r > n:
		return 0
	return fac[n] * finv[n - r] % mod
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]
for i in range(2, max(N + 1, M + 1)):
	fac.append(fac[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	finv.append(finv[-1] * inv[-1] % mod)
c = comb(M, N, mod)
ans = (fac[N] * c % mod) ** 2 % mod
for i in range(1, N + 1):
	ans += (-1) ** i * (comb(N, i, mod) * perm(M, i, mod) % mod * perm(M - i, N - i, mod) ** 2 % mod) % mod
	ans %= mod
print(ans)


def p(n, k, mod):
	x = 1
	for ik in range(k):
		x *= n - ik
		x %= mod
	return x
MAX = 10 ** 9 + 7

def main():
	(n, m) = map(int, input().strip().split())
	s = 0
	nck = 1
	mpk = 1
	mkpnk = p(m, n, MAX)
	for k in range(0, n + 1):
		if 0 < k <= n:
			nck *= (n - (k - 1)) * pow(k, MAX - 2, MAX)
			nck %= MAX
		if 0 < k <= m:
			mpk *= m - (k - 1)
			mpk %= MAX
		if 0 < k <= n:
			mkpnk *= pow(m - (k - 1), MAX - 2, MAX)
			mkpnk %= MAX
		s += nck * (-1) ** k * mpk * mkpnk ** 2
		s %= MAX
	print(s)
main()


def cmb(n, r, p):
	if r < 0 or n < r:
		return 0
	r = min(r, n - r)
	return fact[n] * factinv[r] * factinv[n - r] % p

def per(n, r, p):
	if r < 0 or n < r:
		return 0
	return fact[n] * factinv[n - r] % p
p = 10 ** 9 + 7
N = 5 * pow(10, 5)
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, N + 1):
	fact.append(fact[-1] * i % p)
	inv.append(-inv[p % i] * (p // i) % p)
	factinv.append(factinv[-1] * inv[-1] % p)
(n, m) = map(int, input().split())
ans = 0
for i in range(n + 1):
	ans += pow(per(m - i, n - i, p), 2) * per(m, i, p) * cmb(n, i, p) * (-1) ** i % p
ans = ans % p
print(ans)


class Combi:

	def __init__(self, N, mod):
		self.power = [1 for _ in range(N + 1)]
		self.rev = [1 for _ in range(N + 1)]
		self.mod = mod
		for i in range(2, N + 1):
			self.power[i] = self.power[i - 1] * i % self.mod
		self.rev[N] = pow(self.power[N], self.mod - 2, self.mod)
		for j in range(N, 0, -1):
			self.rev[j - 1] = self.rev[j] * j % self.mod

	def com(self, K, R):
		if K < R:
			return 0
		else:
			return self.power[K] * self.rev[K - R] * self.rev[R] % self.mod

	def pom(self, K, R):
		if K < R:
			return 0
		else:
			return self.power[K] * self.rev[K - R] % self.mod

def main():
	(N, M) = map(int, input().split())
	mod = 10 ** 9 + 7
	c = Combi(5 * 10 ** 5, mod)
	ans = c.pom(M, N) ** 2 % mod
	for k in range(1, N + 1):
		s = (-1) ** ((k - 1) % 2)
		t = c.com(N, k)
		u = c.pom(M - k, N - k) ** 2
		v = c.pom(M, k)
		ans -= s * t * u * v
		ans = ans % mod
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
mod = pow(10, 9) + 7
ans = 0
a = 1
b = 1
c = 1
for i in range(m - n + 1, m + 1):
	b *= i
	b %= mod
for i in range(n + 1):
	k = -1 if i % 2 else 1
	ans += k * a * b ** 2 * c
	ans %= mod
	a *= m - i
	a %= mod
	b *= pow(m - i, mod - 2, mod)
	b %= mod
	c *= n - i
	c *= pow(i + 1, mod - 2, mod)
	c %= mod
print(ans)


def combs_mod(n, k, mod):
	inv = [1] * (k + 1)
	for i in range(1, k + 1):
		inv[i] = pow(i, mod - 2, mod)
	ans = [1] * (k + 1)
	for i in range(1, k + 1):
		ans[i] = ans[i - 1] * (n + 1 - i) * inv[i] % mod
	return ans

def solve():
	ans = 0
	mod = 10 ** 9 + 7
	(N, M) = map(int, input().split())
	NCi = combs_mod(N, N, mod)
	MPi = [1] * (N + 1)
	for i in range(1, N + 1):
		MPi[i] = MPi[i - 1] * (M + 1 - i) % mod
	MiPNi = [1] * (N + 1)
	for i in range(N - 1, -1, -1):
		MiPNi[i] = MiPNi[i + 1] * (M - i) % mod
	for i in range(N + 1):
		ans += pow(-1, i) * NCi[i] * MPi[i] * MiPNi[i] ** 2 % mod
		ans %= mod
	return ans
print(solve())


MOD = 10 ** 9 + 7
(n, m) = list(map(int, input().split()))
U = max(n, m)
fact = [0] * (U + 1)
fact[0] = 1
for i in range(1, U + 1):
	fact[i] = fact[i - 1] * i % MOD
invfact = [0] * (U + 1)
invfact[U] = pow(fact[U], MOD - 2, MOD)
for i in reversed(list(range(U))):
	invfact[i] = invfact[i + 1] * (i + 1) % MOD

def nCr(n, r):
	if r < 0 or n < r:
		return 0
	return fact[n] * invfact[r] * invfact[n - r]

def nPr(n, r):
	return fact[n] * invfact[n - r]
ans = 0
for k in range(n + 1):
	tmp = nCr(n, k) * nPr(m, n) * nPr(m - k, n - k)
	if k % 2 == 0:
		ans += tmp
	else:
		ans -= tmp
	ans %= MOD
print(ans)


MOD = 10 ** 9 + 7
MAXN = 5 * 10 ** 5 + 5
f = [1]
for i in range(MAXN):
	f.append(f[-1] * (i + 1) % MOD)

def nCr(n, r, mod=MOD):
	return f[n] * pow(f[r], mod - 2, mod) * pow(f[n - r], mod - 2, mod) % mod

def nPr(n, r, mod=MOD):
	return f[n] * pow(f[n - r], mod - 2, mod) % mod
(n, m) = map(int, input().split())
ans = 0
for k in range(n + 1):
	tmp = nPr(m - k, n - k) * nPr(m, n) * nCr(n, k) % MOD
	if k % 2:
		ans -= tmp
	else:
		ans += tmp
ans = (ans + MOD) % MOD
print(ans)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, M) = list(map(int, readline().split()))
	COM_MAX = M
	(fac, finv, inv) = ([0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1))
	fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1
	for i in range(2, COM_MAX + 1):
		fac[i] = fac[i - 1] * i % MOD
		inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
		finv[i] = finv[i - 1] * inv[i] % MOD

	def com(n, r):
		if n < 0 or r < 0 or n < r:
			return 0
		return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

	def perm(n, r):
		if n < 0 or r < 0 or n < r:
			return 0
		return fac[n] * finv[n - r] % MOD
	ans = 0
	for k in range(N + 1):
		tmp = perm(M - k, N - k) * com(N, k) % MOD
		if k & 1:
			tmp *= -1
		ans = (ans + tmp) % MOD
	ans = ans * perm(M, N) % MOD
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
ans = 1
for i in range(N):
	ans *= M - i
	ans %= MOD
d = [1, M - N]
for i in range(2, N + 1):
	t = (i - 1) * d[-2] % MOD
	t += (M - N + i - 1) * d[-1] % MOD
	d.append(t)
print(ans * d[-1] % MOD)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
fact = [1, 1]
finv = [1, 1]
inv = [0, 1]
for i in range(2, max(n, m) + 5):
	fact.append(fact[-1] * i % mod)
	inv.append(inv[mod % i] * (mod - mod // i) % mod)
	finv.append(finv[-1] * inv[-1] % mod)

def nCr(n, r, mod):
	if r > n:
		return 0
	else:
		return fact[n] * finv[r] * finv[n - r] % mod

def nPr(n, r, mod):
	if r > n:
		return 0
	else:
		return fact[n] * finv[n - r] % mod
ans = 0
for i in range(n + 1):
	now = nCr(n, i, mod)
	now *= nPr(m - i, n - i, mod)
	now %= mod
	if i % 2:
		now = -now
	ans += now
	ans % mod
ans *= nPr(m, n, mod)
ans %= mod
print(ans)


(n, m) = map(int, input().split())
mod = 1000000007
fact = [1] * (m + 1)
ifact = [1] * (m + 1)
for i in range(m):
	fact[i + 1] = fact[i] * (i + 1) % mod
	ifact[i + 1] = pow(fact[i + 1], mod - 2, mod)

def comb(x, y):
	if y < 0 or y > x:
		return 0
	return fact[x] * ifact[x - y] * ifact[y] % mod

def perm(x, y):
	if y < 0 or y > x:
		return 0
	return fact[x] * ifact[x - y] % mod
ans = 0
for i in range(n + 1):
	ans += (-1) ** i * comb(n, i) % mod * perm(m, i) * (perm(m - i, n - i) % mod) ** 2
	ans %= mod
print(ans)


from functools import reduce
(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
ans = 0
c = 1
p1 = 1
p2 = reduce(lambda ac, v: ac * v % mod, range(m, m - n, -1), 1)
for i in range(n + 1):
	if i > 0:
		c = c * (n - i + 1) * pow(i, mod - 2, mod) % mod
		p1 = p1 * (m - i + 1) % mod
		p2 = p2 * pow(m - i + 1, mod - 2, mod) % mod
	x = c * p1 * p2 ** 2
	if i % 2 == 0:
		ans += x
	else:
		ans -= x
	ans %= mod
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7

def cmb(n, r):
	global mod
	if r < 0 or r > n:
		return 0
	return g1[n] * g2[r] * g2[n - r] % mod

def perm(n, r):
	global mod
	if r > n:
		return g1[n]
	if r < 0:
		return 0
	return g1[n] * g2[n - r] % mod
g1 = [1, 1]
g2 = [1, 1]
inv = [0, 1]
for i in range(2, M + M + 5):
	g1.append(g1[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inv[-1] % mod)
P = 0
for i in range(N + 1):
	P += pow(-1, i) * cmb(N, i) * perm(M, i) * perm(M - i, N - i) ** 2
print(P % mod)


MOD = 10 ** 9 + 7

class Combinatorics:

	def __init__(self, N, mod):
		self.mod = mod
		self.fact = [0] * (N + 1)
		self.inverse = [None] + [0] * N
		self.fact_inverse = [0] * (N + 1)
		self.fact[0] = self.fact[1] = 1
		self.fact_inverse[0] = self.fact_inverse[1] = 1
		self.inverse[1] = 1
		for i in range(2, N + 1):
			self.fact[i] = i * self.fact[i - 1] % self.mod
			(q, r) = divmod(self.mod, i)
			self.inverse[i] = -(q % self.mod) * self.inverse[r] % self.mod
			self.fact_inverse[i] = self.inverse[i] * self.fact_inverse[i - 1] % self.mod

	def perm(self, n, r):
		if n < r or n < 0 or r < 0:
			return 0
		else:
			return self.fact[n] * self.fact_inverse[n - r] % self.mod

	def binom(self, n, r):
		if n < r or n < 0 or r < 0:
			return 0
		else:
			return self.fact[n] * (self.fact_inverse[r] * self.fact_inverse[n - r] % self.mod) % self.mod

	def hom(self, n, r):
		if n == 0 and r > 0:
			return 0
		if n >= 0 and r == 0:
			return 1
		return self.binom(n + r - 1, r)
(N, M) = map(int, input().split())
com = Combinatorics(M, MOD)
ans = 0
for i in range(N + 1):
	temp = com.perm(M, N)
	temp *= com.binom(N, i)
	temp %= MOD
	temp *= com.perm(M - i, N - i)
	temp %= MOD
	ans += (-1) ** i * temp
	ans %= MOD
print(ans)


import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return list(map(int, input().split()))

def LIST():
	return list(map(int, input().split()))

def ZIP(n):
	return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
(N, M) = MAP()
lim = M + 10
fact = [1] * (lim + 1)
fact_inv = [1] * (lim + 1)
for n in range(1, lim + 1):
	fact[n] = fact[n - 1] * n % mod
fact_inv[lim] = pow(fact[lim], mod - 2, mod)
for n in range(lim, 0, -1):
	fact_inv[n - 1] = n * fact_inv[n] % mod

def C(n, r):
	return fact[n] * fact_inv[r] % mod * fact_inv[n - r] % mod

def P(n, r):
	return fact[n] * fact_inv[n - r] % mod
ans = 0
for k in range(N + 1):
	ans += (-1) ** (k % 2) * C(N, k) * P(M - k, N - k)
	ans %= mod
ans *= P(M, N)
print(ans % mod)


(n, m) = list(map(int, input().split()))
mod = 10 ** 9 + 7
import math

def perm(n, r):
	if r > n:
		return 0
	return g1[n] * g2[n - r] % mod

def cmb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
N = 5 * 10 ** 5
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, N + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
ans = 0
for k in range(n + 1):
	a = cmb(n, k, mod)
	p1 = perm(m, k)
	p2 = perm(m - k, n - k)
	ans += a * (-1) ** k * p1 * p2 ** 2
print(ans % mod)


MOD = 10 ** 9 + 7

def facinv(N):
	(fac, finv, inv) = ([0] * (N + 1), [0] * (N + 1), [0] * (N + 1))
	fac[0] = 1
	fac[1] = 1
	finv[0] = 1
	finv[1] = 1
	inv[1] = 1
	for i in range(2, N + 1):
		fac[i] = fac[i - 1] * i % MOD
		inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
		finv[i] = finv[i - 1] * inv[i] % MOD
	return (fac, finv, inv)

def COM(n, r):
	if n < r or r < 0:
		return 0
	else:
		return fac[n] * finv[r] % MOD * finv[n - r] % MOD

def PER(n, r):
	if n < r or r < 0:
		return 0
	else:
		return fac[n] * finv[n - r] % MOD

def p(r):
	return 1 if r % 2 else -1
(N, M) = map(int, input().split())
(fac, finv, inv) = facinv(M)
ans = PER(M, N) ** 2 % MOD
res = 0
for r in range(1, N + 1):
	f = COM(N, r) * PER(M, r) % MOD * (PER(M - r, N - r) ** 2 % MOD) % MOD
	res = (res + p(r) * f + MOD) % MOD
print((ans - res + MOD) % MOD)


mod = 10 ** 9 + 7
P = 500009
Nr = [1 for _ in range(P + 1)]
for i in range(2, P + 1):
	Nr[i] = Nr[i - 1] * i % mod
Dr = [1 for _ in range(P + 1)]
Dr[P] = pow(Nr[P], mod - 2, mod)
for i in range(P, 0, -1):
	Dr[i - 1] = Dr[i] * i % mod

def ncr(n, r):
	if n < r:
		return 0
	else:
		return Nr[n] * Dr[r] % mod * Dr[n - r] % mod

def npr(n, r):
	if n < r:
		return 0
	else:
		return Nr[n] * Dr[n - r] % mod
(n, m) = map(int, input().split())
ans = npr(m, n) ** 2 % mod
for i in range(1, n + 1):
	ans = (ans + (-1) ** (i % 2) * ncr(n, i) % mod * npr(m, i) % mod * npr(m - i, n - i) ** 2 % mod % mod) % mod
print(ans % mod)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
f = [1, 1]
f_inv = [1, 1]
inv = [0, 1]
for i in range(2, m + 1):
	f.append(f[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	f_inv.append(f_inv[-1] * inv[-1] % mod)

def comb(n, k):
	return f[n] * f_inv[k] * f_inv[n - k] % mod

def per(n, k):
	return f[n] * f_inv[n - k] % mod
ans = 0
for i in range(0, n + 1):
	res = comb(n, i) * per(m, i) * per(m - i, n - i) ** 2
	if i % 2 == 1:
		res *= -1
	ans += res
	ans %= mod
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
N = 10 ** 6
fact = [1, 1]
fact_inv = [1, 1]
inv = [0, 1]

def comb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return fact[n] * fact_inv[r] * fact_inv[n - r] % mod

def perm(n, r, mod):
	if r < 0 or r > n:
		return 0
	return fact[n] * fact_inv[n - r] % mod
for i in range(2, N + 1):
	fact.append(fact[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	fact_inv.append(fact_inv[-1] * inv[-1] % mod)
ans = 0
for k in range(n + 1):
	ans += comb(n, k, mod) * perm(m, k, mod) * perm(m - k, n - k, mod) ** 2 * (-1) ** k
	ans %= mod
print(ans)


MOD = int(1000000000.0 + 7)
(N, M) = map(int, input().split())
mod_factorial = [1]
for i in range(1, M + 1):
	mod_factorial.append(mod_factorial[-1] * i % MOD)
mod_fact_inv = [pow(x, MOD - 2, MOD) for x in mod_factorial]

def modperm(n, k):
	return mod_factorial[n] * mod_fact_inv[n - k] % MOD

def modcomb(n, k):
	return modperm(n, k) * mod_fact_inv[k] % MOD
sigma = 0
for i in range(N + 1):
	x = modcomb(N, i)
	x %= MOD
	x *= modperm(M, i)
	x %= MOD
	x *= modperm(M - i, N - i)
	x %= MOD
	x *= modperm(M - i, N - i)
	x %= MOD
	x *= -1 if i & 1 else 1
	sigma += x
sigma %= MOD
print(sigma)


class Factorial:

	def __init__(self, max_fact, mod):
		f = [1] * (max_fact + 1)
		for idx in range(2, max_fact + 1):
			f[idx] = f[idx - 1] * idx
			f[idx] %= mod
		fi = [pow(f[-1], mod - 2, mod)]
		for idx in range(max_fact, 0, -1):
			fi += [fi[-1] * idx % mod]
		fi = fi[::-1]
		self.mod = mod
		self.f = f
		self.fi = fi

	def factorial(self, n):
		return self.f[n]

	def factorial_inverse(self, n):
		return self.fi[n]

	def combination(self, n, r):
		f = self.f
		fi = self.fi
		return f[n] * fi[r] * fi[n - r] % self.mod

	def permutation(self, n, r):
		return self.f[n] * self.fi[n - r] % self.mod

	def homogeneous_product(self, n, r):
		f = self.f
		fi = self.fi
		return f[n + r - 1] * fi[r] * fi[n - 1] % self.mod
max_fact = 10 ** 6
mod = 10 ** 9 + 7
fact_instance = Factorial(max_fact, mod)
comb = fact_instance.combination
perm = fact_instance.permutation
(N, M) = [int(_) for _ in input().split()]
ans = 0
for p in range(N + 1):
	ans += (-1) ** p * comb(N, p) * perm(M - p, N - p)
	ans %= mod
ans *= perm(M, N)
ans %= mod
print(ans)


def main():
	(N, M) = map(int, input().split())
	mod = pow(10, 9) + 7
	g1 = [1, 1]
	g2 = [1, 1]
	inv = [0, 1]
	for i in range(2, max(M, N) + 1):
		g1.append(g1[-1] * i % mod)
		inv.append(-inv[mod % i] * (mod // i) % mod)
		g2.append(g2[-1] * inv[-1] % mod)

	def combi(n, r):
		r = min(r, n - r)
		return g1[n] * g2[r] * g2[n - r] % mod

	def p(n, r):
		return g1[n] * g2[n - r] % mod
	if M < N:
		return 0
	ans = 0
	sgn = 1
	for i in range(N + 1):
		v = sgn * combi(N, i) * p(M, i)
		v %= mod
		v *= p(M - i, N - i) * p(M - i, N - i)
		v %= mod
		ans += v
		ans %= mod
		sgn *= -1
	return ans
print(main())


import sys
sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
mon_l = dict({})

def mon(m, n):
	if (m, n) in mon_l:
		return mon_l[m, n]
	if n == 1:
		mon_l[m, n] = m - 1
		return mon_l[m, n]
	if n == 2:
		mon_l[m, n] = (m - 1 + (m - 2) ** 2) % MOD
		return mon_l[m, n]
	else:
		mon_l[m, n] = ((n - 1) * (mon(m - 1, n - 1) + mon(m - 2, n - 2)) + (m - n) * mon(m - 1, n - 1)) % MOD
		return mon_l[m, n]

def permu(m, n):
	ans = 1
	for _ in range(n):
		ans = m * ans % MOD
		m -= 1
	return ans
(N, M) = map(int, input().split())
ans = permu(M, N) * mon(M, N) % MOD
print(ans)


import sys
MOD = 10 ** 9 + 7
N_MAX = 5 * 10 ** 5
fac = [1, 1]
facinv = [1, 1]
inv = [0, 1]
for i in range(2, N_MAX + 1):
	fac.append(fac[-1] * i % MOD)
	inv.append(-inv[MOD % i] * (MOD // i) % MOD)
	facinv.append(facinv[-1] * inv[-1] % MOD)

def cmb(n, r):
	if r < 0 or r > n:
		return 0
	return fac[n] * facinv[r] * facinv[n - r] % MOD

def prm(n, r):
	if r < 0 or r > n:
		return 0
	return fac[n] * facinv[n - r] % MOD

def main():
	(N, M) = list(map(int, sys.stdin.readline().rstrip().split()))
	all = prm(M, N) ** 2 % MOD
	dup = 0
	for i in range(1, N + 1):
		tmp = prm(M, i) * prm(M - i, N - i) ** 2 % MOD
		tmp *= cmb(N, i) * (-1) ** (i - 1)
		dup += tmp % MOD
	print((all - dup) % MOD)
main()


import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def fact_table(N, mod):
	inv = np.empty(N, np.int64)
	inv[0] = 0
	inv[1] = 1
	for n in range(2, N):
		(q, r) = divmod(mod, n)
		inv[n] = inv[r] * -q % mod
	fact = np.empty(N, np.int64)
	fact[0] = 1
	for n in range(1, N):
		fact[n] = n * fact[n - 1] % mod
	fact_inv = np.empty(N, np.int64)
	fact_inv[0] = 1
	for n in range(1, N):
		fact_inv[n] = fact_inv[n - 1] * inv[n] % mod
	return (fact, fact_inv, inv)

def main():
	(n, m) = list(map(int, readline().split()))
	mod = 10 ** 9 + 7
	(fact, fact_inv, inv) = fact_table(n + 1, mod)
	comb = fact[n] * fact_inv % mod * fact_inv[::-1] % mod
	comb[1::2] *= -1
	comb = comb[::-1]
	p = 1
	ans = 0
	for k in range(n + 1):
		ans += comb[k] * p % mod
		if k != n:
			p = p * (m - n + 1 + k) % mod
	ans %= mod
	print(p * ans % mod)
	return
main()


class Combination:

	def __init__(self, n, mod=10 ** 9 + 7):
		self.mod = mod
		self.fac = [1] * (n + 1)
		for i in range(1, n + 1):
			self.fac[i] = self.fac[i - 1] * i % self.mod
		self.invfac = [1] * (n + 1)
		self.invfac[n] = pow(self.fac[n], self.mod - 2, self.mod)
		for i in range(n - 1, 0, -1):
			self.invfac[i] = self.invfac[i + 1] * (i + 1) % self.mod

	def combination(self, n, r):
		return self.fac[n] * self.invfac[r] % self.mod * self.invfac[n - r] % self.mod

	def permutation(self, n, r):
		return self.factorial(n) * self.invfactorial(n - r) % self.mod

	def factorial(self, i):
		return self.fac[i]

	def invfactorial(self, i):
		return self.invfac[i]

def main():
	import sys

	def input():
		return sys.stdin.readline().rstrip()
	(n, m) = map(int, input().split())
	c = Combination(m)
	mod = 10 ** 9 + 7
	ans = 0
	for i in range(n + 1):
		tmp = c.combination(n, i) * pow(-1, i % 2) * c.permutation(m, i) % mod
		tmp *= c.permutation(m - i, n - i) ** 2
		tmp %= mod
		ans += tmp
		ans %= mod
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
p = 10 ** 9 + 7
a = 1
fct = [a]
for i in range(1, m + 1):
	a = a * i % p
	fct.append(a)
b = pow(fct[-1], p - 2, p)
inv = [b]
for i in range(1, m + 1)[::-1]:
	b = b * i % p
	inv.append(b)
inv.reverse()
ans = fct[m] * inv[m - n] % p
tmp = 0
for i in range(1, n + 1):
	tmp += (-1) ** (i - 1) * fct[n] * inv[n - i] * inv[i] * fct[m - i] * inv[m - n] % p
	tmp %= p
tmp = (fct[m] * inv[m - n] - tmp) % p
print(ans * tmp % p)


(n, m) = map(int, input().split())
(M, F) = (10 ** 9 + 7, [1])
for i in range(m):
	F += [-~i * F[i] % M]
c = lambda n, r: F[n] * pow(F[r] * F[n - r], M - 2, M)
p = lambda n, r: F[n] * pow(F[n - r], M - 2, M)
print(sum(((-1) ** k * c(n, k) * p(m, k) * p(m - k, n - k) ** 2 for k in range(n + 1))) % M)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
FCT = [1]
for i in range(1, M + 1):
	FCT.append(FCT[-1] * i % MOD)

def pmu(n, r, mod=MOD):
	return FCT[n] * pow(FCT[n - r], mod - 2, mod) % mod

def cmb(n, r, mod=MOD):
	return pmu(n, r) * pow(FCT[r], mod - 2, mod) % mod

def solve():
	ans = 0
	for i in range(N + 1):
		cp = cmb(N, i) * pmu(M - i, N - i) % MOD
		ans = (ans + (1 if i % 2 == 0 else -1) * cp) % MOD
	return ans * pmu(M, N) % MOD
print(solve())


class Factorial:

	def __init__(self, n, mod):
		self.mod = mod
		self.fct = [0 for _ in range(n + 1)]
		self.inv = [0 for _ in range(n + 1)]
		self.fct[0] = 1
		self.inv[0] = 1
		for i in range(n):
			self.fct[i + 1] = self.fct[i] * (i + 1) % mod
		self.inv[n] = pow(self.fct[n], mod - 2, mod)
		for i in range(n)[::-1]:
			self.inv[i] = self.inv[i + 1] * (i + 1) % mod

	def fact(self, m):
		return self.fct[m]

	def invf(self, m):
		return self.inv[m]

	def perm(self, m, k):
		if m < k:
			return 0
		return self.fct[m] * self.inv[m - k] % self.mod

	def comb(self, m, k):
		if m < k:
			return 0
		return self.fct[m] * self.inv[k] * self.inv[m - k] % self.mod
MOD = 1000000007
(N, M) = map(int, input().split())
f = Factorial(M, MOD)
res = f.perm(M, N)
for k in range(1, N + 1):
	res -= (-1) ** (k - 1) * f.comb(N, k) * f.perm(M - k, N - k)
res *= f.perm(M, N)
res %= MOD
print(res)


(n, m) = map(int, input().split())
MOD = 10 ** 9 + 7
FAC = [1]
INV = [1]
for i in range(1, m + 1):
	FAC.append(FAC[i - 1] * i % MOD)
	INV.append(pow(FAC[-1], MOD - 2, MOD))

def nCr(n, r):
	return FAC[n] * INV[n - r] * INV[r] % MOD

def nPr(n, r):
	return FAC[n] * INV[n - r] % MOD
ans = 0
for i in range(n + 1):
	now = nCr(n, i)
	now *= nPr(m, i)
	now %= MOD
	now *= pow(nPr(m - i, n - i), 2, MOD)
	now %= MOD
	now *= pow(-1, i, MOD)
	now %= MOD
	ans += now
	ans %= MOD
print(ans)


class CombMod:

	def __init__(self, V, p):
		self.fact = [1] * V
		self.finv = [1] * V
		for i in range(1, V):
			self.fact[i] = self.fact[i - 1] * i % p
		self.finv[-1] = pow(self.fact[-1], p - 2, p)
		for i in range(V - 2, 0, -1):
			self.finv[i] = self.finv[i + 1] * (i + 1) % p
		self.p = p

	def fact(self, a):
		return self.fact[a]

	def finv(self, a):
		return self.finv[a]

	def comb(self, a, b):
		return self.fact[a] * self.finv[b] % self.p * self.finv[a - b] % self.p

	def perm(self, a, b):
		return self.fact[a] * self.finv[a - b] % self.p

	def hcomb(self, a, b):
		return self.comb(a + b - 1, b)
(N, M) = list(map(int, input().split()))
mod = 10 ** 9 + 7
tool = CombMod(max(N, M) + 10, mod)
ans = 0
for i in range(0, N + 1):
	p1 = tool.perm(M, N - i)
	p2 = tool.perm(M - (N - i), i)
	g_i = p1 * p2 % mod * p2 % mod
	val = pow(-1, N - i, mod) * tool.comb(N, i) % mod * g_i % mod
	ans = (ans + val) % mod
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7

def c(a, b):
	r = fac[b] * fac[a - b] % mod
	return fac[a] * pow(r, mod - 2, mod) % mod

def p(a, b):
	return c(a, b) * fac[b] % mod
fac = [1]
for i in range(m + 2):
	fac.append(fac[-1] * (i + 1) % mod)
ans = p(m, n)
for i in range(1, n + 1):
	r = c(n, i) * p(m - i, n - i) % mod
	if i % 2 == 1:
		ans -= r
	else:
		ans += r
	ans %= mod
print(ans * p(m, n) % mod)


def main():
	import sys
	imput = sys.stdin.readline
	(n, m) = list(map(int, input().split()))
	mod = pow(10, 9) + 7
	n_ = 5 * pow(10, 5) + 5
	fun = [1] * (n_ + 1)
	for i in range(1, n_ + 1):
		fun[i] = fun[i - 1] * i % mod
	rev = [1] * (n_ + 1)
	rev[n_] = pow(fun[n_], mod - 2, mod)
	for i in range(n_ - 1, 0, -1):
		rev[i] = rev[i + 1] * (i + 1) % mod

	def cmb(n, r):
		if n < 0 or r < 0 or r > n:
			return 0
		return fun[n] * rev[r] % mod * rev[n - r] % mod

	def perm(n, r):
		if n < 0 or r < 0 or r > n:
			return 0
		return fun[n] * rev[n - r] % mod
	import math
	answer = 0
	for i in range(n + 1):
		temp = perm(n, i) * cmb(m, i) * pow(perm(m - i, n - i), 2)
		temp %= mod
		if i % 2 == 0:
			answer += temp
		else:
			answer -= temp
	print(answer % mod)

def __starting_point():
	main()
__starting_point()


(N, M) = [int(i) for i in input().split()]
P = 10 ** 9 + 7
nn = M + 1
fa = [1] * (nn + 1)
fainv = [1] * (nn + 1)
for i in range(nn):
	fa[i + 1] = fa[i] * (i + 1) % P
fainv[-1] = pow(fa[-1], P - 2, P)
for i in range(nn)[::-1]:
	fainv[i] = fainv[i + 1] * (i + 1) % P
C = lambda a, b: fa[a] * fainv[b] % P * fainv[a - b] % P if 0 <= b <= a else 0
ans = (fa[M] * fainv[M - N] % P) ** 2 % P
for i in range(1, N + 1):
	ans += (-1) ** i * C(N, i) * fa[M] % P * fa[M - i] % P * fainv[M - N] % P * fainv[M - N] % P
	ans %= P
print(ans)


(n, m) = map(int, input().split())
mod = pow(10, 9) + 7
x = 0
y = m - n + 1
(u, v) = (n, 1)
(c, p) = (1, 1)
for i in range(n, 0, -1):
	if i % 2 == 1:
		x += c * p % mod
	else:
		x -= c * p % mod
	x %= mod
	p *= y
	p %= mod
	y += 1
	c = c * u * pow(v, mod - 2, mod) % mod
	u -= 1
	v += 1
p = 1
for i in range(n):
	p *= m - i
	p %= mod
ans = (pow(p, 2, mod) - p * x) % mod
print(ans)


(n, m) = map(int, input().split())
fct = [1, 1] + [0] * m
mod = 10 ** 9 + 7
for i in range(2, m + 1):
	fct[i] = fct[i - 1] * i
	fct[i] %= mod
inv = [1, 1] + [0] * m
invf = [1, 1] + [0] * m
for i in range(2, m + 1):
	inv[i] = -inv[mod % i] * (mod // i) % mod
	invf[i] = invf[i - 1] * inv[i] % mod
ans = 0

def chs(x, y):
	return fct[x] * invf[y] * invf[x - y]

def chs2(x, y):
	return fct[x] * invf[x - y]
m_inv = pow(m, mod - 2, mod)
mn = pow(m, n, mod)
for i in range(n + 1):
	ans += (-1) ** (i % 2) * chs(n, i) * chs2(m - i, n - i)
	ans %= mod
	mn *= m_inv
	mn %= mod
print(ans * chs2(m, n) % mod)


import sys

def S():
	return sys.stdin.readline().rstrip()
(N, M) = map(int, S().split())
mod = 10 ** 9 + 7
a = M
for i in range(1, N):
	a *= M - i
	a %= mod
ans = a ** 2 % mod
r = ans
for i in range(1, N + 1):
	r *= (N - i + 1) * pow(i, mod - 2, mod) * pow(M - i + 1, mod - 2, mod) * -1
	r %= mod
	ans += r
	ans %= mod
print(ans)


import math
MOD = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

def init(n):
	for i in range(2, n + 1):
		fact.append(fact[-1] * i % MOD)
		inv.append(-inv[MOD % i] * (MOD // i) % MOD)
		factinv.append(factinv[-1] * inv[-1] % MOD)

def nPk(n, k):
	return fact[n] * factinv[n - k] % MOD

def nCk(n, k):
	return fact[n] * factinv[n - k] * factinv[k] % MOD
(N, M) = list(map(int, input().split()))
init(M)
ans = 0
for k in range(N + 1):
	if k % 2 == 0:
		ans += nCk(N, k) * nPk(M - k, N - k)
	else:
		ans -= nCk(N, k) * nPk(M - k, N - k)
	ans = (ans + MOD) % MOD
ans *= nPk(M, N)
ans %= MOD
print(ans)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
n = N + M
fac = [1] * (n + 1)
inv = [1] * (n + 1)
for j in range(1, n + 1):
	fac[j] = fac[j - 1] * j % MOD
inv[n] = pow(fac[n], MOD - 2, MOD)
for j in range(n - 1, -1, -1):
	inv[j] = inv[j + 1] * (j + 1) % MOD

def comb(n, r):
	if r > n or n < 0 or r < 0:
		return 0
	return fac[n] * inv[n - r] * inv[r] % MOD

def perm(n, r):
	if r > n or n < 0 or r < 0:
		return 0
	return comb(n, r) * fac[r] % MOD
ans = pow(perm(M, N), 2, MOD)
for r in range(1, N + 1):
	p = comb(M, r) * perm(N, r) * pow(perm(M - r, N - r), 2, MOD) % MOD
	p *= -(-1) ** (r % 2)
	ans -= p
	ans %= MOD
print(ans)


MAX = 5 * 10 ** 5
MOD = 10 ** 9 + 7
_fact = [1, 1] + [0] * MAX
_inv = [0, 1] + [0] * MAX
_fact_inv = [1, 1] + [0] * MAX
for k in range(2, MAX + 1):
	_fact[k] = _fact[k - 1] * k % MOD
	_inv[k] = -_inv[MOD % k] * (MOD // k) % MOD
	_fact_inv[k] = _fact_inv[k - 1] * _inv[k] % MOD

def P(m, n):
	return _fact[m] * _fact_inv[m - n] % MOD

def C(m, n):
	return _fact[m] * _fact_inv[m - n] * _fact_inv[n] % MOD

def solve(n, m):
	res = 0
	for k in range(n + 1):
		sign = 1 - 2 * (k % 2)
		cur = sign * C(n, k) * P(m, k) * P(m - k, n - k) ** 2
		res = (res + cur) % MOD
	return res
(n, m) = map(int, input().split())
print(solve(n, m))


class Factorials:

	def __init__(self, n, mod):
		self.mod = mod
		self.fac = [1]
		num = 1
		for i in range(1, n + 1):
			num *= i
			num %= mod
			self.fac.append(num)
		num = pow(num, mod - 2, mod)
		self.rec = [1 for i in range(n + 1)]
		self.rec[n] = num
		for i in range(n - 1, 0, -1):
			num *= i + 1
			num %= mod
			self.rec[i] = num

	def comb(self, n, r):
		return self.fac[n] * self.rec[r] * self.rec[n - r] % self.mod

	def perm(self, n, r):
		return self.fac[n] * self.rec[n - r] % self.mod

def main():
	(n, m) = map(int, input().split())
	mod = 10 ** 9 + 7
	f = Factorials(m, mod)
	ans = 0
	for i in range(n + 1):
		if i % 2 == 0:
			ans += f.comb(n, i) * f.perm(m, i) * f.perm(m - i, n - i) * f.perm(m - i, n - i) % mod
		else:
			ans -= f.comb(n, i) * f.perm(m, i) * f.perm(m - i, n - i) * f.perm(m - i, n - i) % mod
		ans %= mod
	print(ans)
main()


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7

def cmb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, M + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
ans = 0
for k in range(N + 1):
	ans += cmb(N, k, mod) * (-1) ** k * cmb(M, k, mod) * g1[k] * (cmb(M - k, N - k, mod) * g1[N - k]) ** 2
	ans %= mod
print(ans)


P = 10 ** 9 + 7
(N, M) = map(int, input().split())
d = [0] * (M + 1)
d[0] = 1
for i in range(1, M + 1):
	d[i] = d[i - 1] * i % P
ans = d[M] * pow(d[M - N], P - 2, P)
ans **= 2
ans %= P
for i in range(1, N + 1):
	temp = d[N] * pow(d[N - i], P - 2, P) * pow(d[i], P - 2, P) % P
	temp *= d[M]
	temp %= P
	temp *= pow(d[M - i], P - 2, P)
	temp %= P
	temp *= (d[M - i] * pow(d[max(M - N, 1)], P - 2, P)) ** 2 % P
	ans += temp % P * (-1) ** i
	ans %= P
print(ans % P)


(N, M) = map(int, input().split())
list = [0 for i in range(5 * 10 ** 5 + 1)]
mod = 10 ** 9 + 7
list[0] = 1
for i in range(len(list) - 1):
	list[i + 1] = list[i] * (i + 1) % mod

def c(m, n):
	a = list[m] * pow(list[m - n], mod - 2, mod) * pow(list[n], mod - 2, mod)
	return a

def p(m, n):
	a = list[m] * pow(list[m - n], mod - 2, mod)
	return a
ans = pow(p(M, N), 2)
g = pow(list[M - N], mod - 2, mod)
g2 = pow(g, 2, mod)
nn = list[N]
mm = list[M]
nmg = nn * mm * g2 % mod
for i in range(1, N + 1):
	ans += nmg * pow(list[N - i], mod - 2, mod) * pow(list[i], mod - 2, mod) * list[M - i] * (-1) ** i
	ans %= mod
print(ans)


def prepare(n):
	nonlocal mod
	modFacts = [0] * (n + 1)
	modFacts[0] = 1
	for i in range(n):
		modFacts[i + 1] = modFacts[i] * (i + 1) % mod
	invs = [1] * (n + 1)
	invs[n] = pow(modFacts[n], mod - 2, mod)
	for i in range(n, 1, -1):
		invs[i - 1] = invs[i] * i % mod
	return (modFacts, invs)
(n, m) = list(map(int, input().split()))
mod = 10 ** 9 + 7
(modFacts, invs) = prepare(m)
ans = 0
nci = 0
for i in range(n + 1):
	nci = modFacts[n] * invs[n - i] * invs[i] % mod
	mpi = modFacts[m] * invs[m - i] % mod
	mipni = modFacts[m - i] * invs[m - n] % mod
	ans += nci * mpi * mipni ** 2 % mod * (-1) ** i
	ans %= mod
print(ans)


from sys import stdin
input = stdin.readline
MOD = 10 ** 9 + 7

def solve():
	(N, M) = map(int, input().split())
	fac = [1, 1] + [0] * M
	inv = [1, 1] + [0] * M
	for i in range(2, M + 1):
		fac[i] = fac[i - 1] * i % MOD
	inv[M] = pow(fac[M], MOD - 2, MOD)
	for i in range(M - 1, 1, -1):
		inv[i] = inv[i + 1] * (i + 1) % MOD

	def p(a, b):
		return fac[b] * inv[b - a] % MOD

	def c(a, b):
		return fac[b] * inv[a] * inv[b - a] % MOD
	res = p(N, M)
	for i in range(1, N + 1):
		res += (-1) ** (i & 1) * c(i, N) * p(N - i, M - i)
		res %= MOD
	print(res * p(N, M) % MOD)
solve()


def main():
	mod = 1000000007
	inv = lambda x: pow(x, mod - 2, mod)
	(n, m) = map(int, input().split())
	Fact = [1]
	for i in range(1, m + 1):
		Fact.append(Fact[i - 1] * i % mod)
	Finv = [0] * (m + 1)
	Finv[-1] = inv(Fact[-1])
	for i in range(m - 1, -1, -1):
		Finv[i] = Finv[i + 1] * (i + 1) % mod

	def comb(n, r):
		if n < r:
			return 0
		return Fact[n] * Finv[r] * Finv[n - r] % mod

	def perm(n, r):
		if n < r:
			return 0
		return Fact[n] * Finv[n - r] % mod
	ans = 0
	p = 1
	for i in range(n + 1):
		ans += comb(n, i) * perm(m, i) * perm(m - i, n - i) ** 2 * p
		ans %= mod
		p *= -1
	print(ans)
main()


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
fact = [1] * (M + 1)
fact_inv = [1] * (M + 1)
for i in range(1, M + 1):
	fact[i] = i * fact[i - 1] % mod
fact_inv[M] = pow(fact[M], mod - 2, mod)
for i in range(1, M + 1)[::-1]:
	fact_inv[i - 1] = i * fact_inv[i] % mod
comb = lambda n, k: fact[n] * fact_inv[k] * fact_inv[n - k] % mod
ans = comb(M, N) * fact[N] % mod
for k in range(1, N + 1):
	ans += (-1) ** k * comb(N, k) * fact[M - k] * fact_inv[M - N] % mod
	ans %= mod
ans = ans * comb(M, N) * fact[N] % mod
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7

def setfact(l, mod):
	global fact
	global ifact
	fact = [1]
	for i in range(1, l + 1):
		fact.append(fact[-1] * i % mod)
	ifact = [0] * (l + 1)
	ifact[-1] = pow(fact[-1], mod - 2, mod)
	for i in range(l, 0, -1):
		ifact[i - 1] = ifact[i] * i % mod

def comb(n, m, mod):
	global fact
	global ifact
	return fact[n] * ifact[n - m] * ifact[m] % mod

def p(n, m, mod):
	global fact
	global ifact
	return fact[n] * ifact[n - m] % mod
ans = 0
setfact(m, mod)
for k in range(n + 1):
	ans += p(m - k, n - k, mod) * comb(n, k, mod) * (-1) ** k
	ans %= mod
	if ans < 0:
		ans += mod
ans *= p(m, n, mod)
ans %= mod
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
ans = M
for i in range(M - 1, M - N, -1):
	ans = ans * i % mod
prev = 1
ansB = M - 1
for i in range(1, N):
	tmp = prev * i % mod
	tmp2 = ansB - tmp % mod
	ansB = tmp * (M - i) % mod
	ansB += tmp2 * (M - i - 1) % mod
	ansB %= mod
	prev = tmp2
print(ans * ansB % mod)


import sys

def input():
	return sys.stdin.readline().strip()

def mapint():
	return map(int, input().split())
sys.setrecursionlimit(10 ** 9)
(N, M) = mapint()
mod = 10 ** 9 + 7
pos = [1] + [0] * M
neg = [1] + [0] * M
n = 1
for i in range(1, M + 1):
	n = n * i % mod
	pos[i] = n
	neg[i] = pow(n, mod - 2, mod)
ans = 0

def comb(n, r):
	return pos[n] * neg[r] * neg[n - r] % mod
for i in range(N + 1):
	ans += (-1) ** i * comb(N, i) * pos[M] * neg[M - i] * pow(pos[M - i] * neg[M - N], 2, mod)
	ans %= mod
print(ans)


def main():
	(n, m) = list(map(int, input().split()))
	MOD = 10 ** 9 + 7
	fact = [1]
	fact_inv = [1]
	for i in range(m):
		fact.append(fact[-1] * (i + 1) % MOD)
	fact_inv = [1] * (m + 1)
	fact_inv[m] = pow(fact[m], MOD - 2, MOD)
	for i in range(m):
		fact_inv[m - i - 1] = fact_inv[m - i] * (m - i) % MOD
	mPn = fact[m] * pow(fact[m - n], MOD - 2, MOD) % MOD
	a = pow(mPn, 2, MOD)
	b = 0
	for i in range(1, n + 1):
		nCr = fact[n] * fact_inv[i] % MOD * fact_inv[n - i] % MOD
		b += (-1) ** (i % 2) * nCr * fact[m - i] % MOD * fact_inv[m - n] % MOD
		b %= MOD
	ans = a + b * mPn
	print(ans % MOD)
main()


class FermatCmb:

	def __init__(self, max_num, mod):
		self.max_num = max_num
		self.mod = mod
		self.fact = [0 for _ in range(max_num + 1)]
		self.factinv = [0 for _ in range(max_num + 1)]
		self.fact[0] = 1
		for i in range(1, max_num + 1):
			self.fact[i] = i * self.fact[i - 1] % self.mod
		self.factinv[-1] = pow(self.fact[-1], mod - 2, mod)
		for i in range(max_num, 0, -1):
			self.factinv[i - 1] = self.factinv[i] * i
			self.factinv[i - 1] %= self.mod

	def nCk(self, n, k):
		return self.fact[n] * self.factinv[k] * self.factinv[n - k] % self.mod

	def nPk(self, n, k):
		return self.fact[n] * self.factinv[n - k] % self.mod

def solve(N, M):
	mod = 10 ** 9 + 7
	cf = FermatCmb(M, mod)
	ans = 0
	for k in range(N + 1):
		ans += (-1) ** k * cf.nCk(N, k) * cf.nPk(M, k) * cf.nPk(M - k, N - k) ** 2
		ans %= mod
	print(ans)

def __starting_point():
	(N, M) = list(map(int, input().split()))
	solve(N, M)
__starting_point()


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
ans1 = 1
for i in range(N):
	ans1 *= M - i
	ans1 %= MOD
ans2 = [1, M - N]
for i in range(2, N + 1):
	ans2.append((ans2[-2] * (i - 1) + ans2[-1] * (M - N + i - 1)) % MOD)
print(ans1 * ans2[N] % MOD)


max_fact = 5 * 10 ** 5
mod = 10 ** 9 + 7
f = [1] * (max_fact + 1)
for idx in range(2, max_fact + 1):
	f[idx] = f[idx - 1] * idx
	f[idx] %= mod
fi = [pow(f[-1], mod - 2, mod)]
for idx in range(max_fact, 0, -1):
	fi += [fi[-1] * idx % mod]
fi = fi[::-1]

def factorial(n):
	return f[n]

def factorial_inverse(n):
	return fi[n]

def combination(n, r):
	return f[n] * fi[r] * fi[n - r] % mod

def permutation(n, r):
	return f[n] * fi[n - r] % mod

def homogeneous_product(n, r):
	return f[n + r - 1] * fi[r] * fi[n - 1] % mod
comb = combination
perm = permutation
(N, M) = [int(_) for _ in input().split()]
ans = 0
for p in range(N + 1):
	ans += (-1) ** p * comb(N, p) * perm(M - p, N - p)
	ans %= mod
ans *= perm(M, N)
ans %= mod
print(ans)


def prepare(n):
	nonlocal MOD
	modFacts = [0] * (n + 1)
	modFacts[0] = 1
	for i in range(n):
		modFacts[i + 1] = modFacts[i] * (i + 1) % MOD
	invs = [1] * (n + 1)
	invs[n] = pow(modFacts[n], MOD - 2, MOD)
	for i in range(n, 1, -1):
		invs[i - 1] = invs[i] * i % MOD
	return (modFacts, invs)
(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
(modFacts, invs) = prepare(max(N, M))
ans = 0
for i in range(N + 1):
	Ti = modFacts[N] * invs[i] * invs[N - i] % MOD
	Ti *= modFacts[M] * invs[M - i] % MOD
	Ti %= MOD
	Ti *= pow(modFacts[M - i] * invs[M - i - (N - i)], 2, MOD)
	Ti %= MOD
	ans += pow(-1, i) * Ti
	ans %= MOD
print(ans)


mod = 10 ** 9 + 7
(N, M) = map(int, input().split())
R = [1] * (M + 1)
for i in range(2, M + 1):
	R[i] = R[i - 1] * i
	R[i] %= mod
ans = 0
for k in range(N + 1):
	a = (-1) ** k
	a *= R[N] * R[M] * R[M - k]
	a %= mod
	a *= pow(R[k], mod - 2, mod)
	a *= pow(R[N - k], mod - 2, mod)
	a %= mod
	a *= pow(R[M - N], mod - 2, mod) ** 2
	a %= mod
	ans += a
print(ans % mod)


MOD = 10 ** 9 + 7
MAX = 5 * 10 ** 5 + 1
fact = [0 for _ in range(MAX)]
factinv = [0 for _ in range(MAX)]
fact[0] = 1
for k in range(1, MAX):
	fact[k] = fact[k - 1] * k
	fact[k] %= MOD
factinv[MAX - 1] = pow(fact[MAX - 1], MOD - 2, MOD)
for k in range(MAX - 1, 0, -1):
	factinv[k - 1] = factinv[k] * k
	factinv[k - 1] %= MOD

def nCk(n, k):
	return fact[n] * factinv[k] * factinv[n - k] % MOD

def nPk(n, k):
	return fact[n] * factinv[n - k] % MOD
(n, m) = map(int, input().split(' '))
ans = 0
for k in range(n + 1):
	tmp = nCk(n, k) * nPk(m, k) * nPk(m - k, n - k) * nPk(m - k, n - k) % MOD
	if not k % 2:
		ans += tmp
	else:
		ans -= tmp
	ans %= MOD
print(ans)


def cmb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, m + 2):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
f = [0, 1]
for i in range(2, n + 2):
	f.append(f[-1] * i % mod)
x = [1]
for i in range(m - n + 1, m + 1):
	x.append(x[-1] * i % mod)
x.reverse()
ans = 0
for i in range(len(x)):
	if i % 2 == 1:
		ans -= cmb(n, i, mod) * x[i]
	else:
		ans += cmb(n, i, mod) * x[i]
	ans %= mod
print(ans * f[n] * cmb(m, n, mod) % mod)


(n, m) = map(int, input().split())
(i, d) = (0, [1] * (n + 1))
while i < n:
	d[i + 1] = (m - i) * ((m - n + i) * d[i] + i * d[i - 1] * (m - i + 1)) % (10 ** 9 + 7)
	i += 1
print(d[-1])


def makefac(n):
	global faclist
	faclist = [1, 1]
	for i in range(2, n + 1):
		faclist.append(faclist[-1] * i % MOD)
	return None

def mcomb(n, r):
	return faclist[n] * pow(faclist[r], MOD - 2, MOD) * pow(faclist[n - r], MOD - 2, MOD)

def mperm(n, r):
	return faclist[n] * pow(faclist[n - r], MOD - 2, MOD)
(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
makefac(M)
ans = mperm(M, N)
for k in range(1, N + 1):
	ans += (-1) ** k * mcomb(N, k) * mperm(M - k, N - k) % MOD
ans = ans * mperm(M, N) % MOD
print(ans)


class Factorial:

	def __init__(self, n, mod):
		self.f = [1]
		self.mod = mod
		for j in range(1, n + 1):
			self.f.append(self.f[-1] * j % mod)
		self.i = [pow(self.f[-1], mod - 2, mod)]
		for j in range(n, 0, -1):
			self.i.append(self.i[-1] * j % mod)
		self.i.reverse()

	def factorial(self, j):
		return self.f[j]

	def ifactorial(self, j):
		return self.i[j]

	def comb(self, n, k):
		return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0
(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
F = Factorial(M + 1, MOD)
ans = 0
for k in range(N + 1):
	tmp1 = F.comb(N, k)
	tmp2 = F.factorial(M) * F.ifactorial(M - k) % MOD
	tmp3 = (F.factorial(M - k) * F.ifactorial(M - N)) ** 2 % MOD
	tmp = tmp1 * tmp2 * tmp3 % MOD
	if k % 2 == 0:
		ans += tmp
	else:
		ans -= tmp
	ans %= MOD
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

def cmb(n, r):
	if r < 0 or n < r:
		return 0
	r = min(r, n - r)
	return fact[n] * factinv[r] * factinv[n - r] % mod

def perm(n, r):
	if r < 0 or n < r:
		return 0
	return fact[n] * factinv[n - r] % mod
for i in range(2, M + 1):
	fact.append(fact[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	factinv.append(factinv[-1] * inv[-1] % mod)
ans = 0
for i in range(N + 1):
	ans += cmb(N, i) * perm(M - i, N - i) * (-1) ** i
	ans %= mod
ans *= perm(M, N)
ans %= mod
print(ans)


import sys, math
input = sys.stdin.readline
rs = lambda : input().strip()
ri = lambda : int(input())
rl = lambda : list(map(int, input().split()))
mod = 10 ** 9 + 7

class CombMod:

	def __init__(self, N):
		self.fact = [1]
		self.rfact = [1]
		for i in range(1, N + 1):
			self.fact.append(self.fact[i - 1] * i % mod)
			self.rfact.append(pow(self.fact[i], mod - 2, mod))

	def comb(self, n, k):
		return self.fact[n] * self.rfact[k] * self.rfact[n - k] % mod

	def perm(self, n, k):
		return self.fact[n] * self.rfact[n - k] % mod
(N, M) = rl()
cm = CombMod(M)
ans = 0
for i in range(0, N + 1):
	ans += (-1) ** i * cm.perm(M, i) * cm.perm(M - i, N - i) ** 2 * cm.comb(N, i)
	ans = ans % mod
print(ans)


(n, m) = [int(x) for x in input().split(' ')]
MOD = int(1000000000.0 + 7)
fact = [0 for _ in range(m + 1)]
fact[0] = 1
for i in range(1, m + 1):
	fact[i] = fact[i - 1] * i % MOD
finv = [0 for _ in range(m + 1)]
finv[m] = pow(fact[m], MOD - 2, MOD)
for i in range(m - 1, -1, -1):
	finv[i] = finv[i + 1] * (i + 1) % MOD

def nCk(n, k):
	return fact[n] * finv[k] * finv[n - k] % MOD

def nPk(n, k):
	return fact[n] * finv[n - k] % MOD
ans = 0
for k in range(0, n + 1):
	tmp = nCk(n, k) * nPk(m, k) * nPk(m - k, n - k) ** 2
	if k % 2 == 0:
		ans += tmp
	else:
		ans -= tmp
	ans = ans % MOD
print(ans)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
fact = [1] * (M + 1)
for i in range(2, M + 1):
	fact[i] = i * fact[i - 1] % MOD
invfact = [1] * (M + 1)
invfact[M] = pow(fact[M], MOD - 2, MOD)
for i in range(M, 1, -1):
	invfact[i - 1] = invfact[i] * i % MOD

def permute(n, k):
	return fact[n] * invfact[n - k] % MOD

def choose(n, k):
	return permute(n, k) * invfact[k] % MOD
total = pow(permute(M, N), 2, MOD)
add = -1
for i in range(1, N + 1):
	total = (total + add * choose(N, i) * permute(M, i) * pow(permute(M - i, N - i), 2, MOD)) % MOD
	add *= -1
print(total)


def prepare(n, MOD):
	f = 1
	fn = [1] * (n + 1)
	for m in range(1, n + 1):
		f *= m
		f %= MOD
		fn[m] = f
	inv = pow(f, MOD - 2, MOD)
	invs = [1] * (n + 1)
	invs[n] = inv
	for m in range(n, 1, -1):
		inv *= m
		inv %= MOD
		invs[m - 1] = inv
	return (fn, invs)

def main():
	(n, m) = map(int, input().split())
	MOD = 10 ** 9 + 7
	(fn, invs) = prepare(m, MOD)
	ans = 0
	for i in range(n + 1):
		x = fn[m - i] * invs[m - n] % MOD
		x = x * fn[n] * invs[i] * invs[n - i] % MOD
		x *= (-1) ** i
		ans += x
	ans = ans * fn[m] * invs[m - n] % MOD
	print(ans)

def __starting_point():
	main()
__starting_point()


MOD = 10 ** 9 + 7
table_len = 500010
fac = [1, 1]
for i in range(2, table_len):
	fac.append(fac[-1] * i % MOD)
finv = [0] * table_len
finv[-1] = pow(fac[-1], MOD - 2, MOD)
for i in range(table_len - 1, -1, -1):
	finv[i - 1] = finv[i] * i % MOD

def comb(n, k):
	return fac[n] * finv[k] * finv[n - k] % MOD

def perm(n, k):
	return fac[n] * finv[n - k] % MOD
(N, M) = list(map(int, input().split()))
print(sum(((-1) ** k * comb(N, k) * perm(M, k) * perm(M - k, N - k) ** 2 % MOD for k in range(N + 1))) % MOD)


def main():
	import sys
	imput = sys.stdin.readline
	if sys.argv[-1] == 'ONLINE_JUDGE':
		import numba
		from numba.pycc import CC
		i8 = numba.int64
		cc = CC('my_module')

		def cc_export(f, signature):
			cc.export(f.__name__, signature)(f)
			return numba.njit(f)
			fact_table = cc_export(fact_table, (i8, i8))
			main = cc_export(main, (i8, i8, i8))
			cc.compile()
	(n, m) = list(map(int, input().split()))
	mod = pow(10, 9) + 7
	n_ = 5 * pow(10, 5) + 5
	fun = [1] * (n_ + 1)
	for i in range(1, n_ + 1):
		fun[i] = fun[i - 1] * i % mod
	rev = [1] * (n_ + 1)
	rev[n_] = pow(fun[n_], mod - 2, mod)
	for i in range(n_ - 1, 0, -1):
		rev[i] = rev[i + 1] * (i + 1) % mod

	def cmb(n, r):
		if n < 0 or r < 0 or r > n:
			return 0
		return fun[n] * rev[r] % mod * rev[n - r] % mod

	def perm(n, r):
		if n < 0 or r < 0 or r > n:
			return 0
		return fun[n] * rev[n - r] % mod
	import math
	answer = 0
	for i in range(n + 1):
		temp = perm(n, i) * cmb(m, i) * pow(perm(m - i, n - i), 2)
		temp %= mod
		if i % 2 == 0:
			answer += temp
		else:
			answer -= temp
	print(answer % mod)

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 8)

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return list(map(int, input().split()))

def LIST():
	return list(map(int, input().split()))

def ZIP(n):
	return list(zip(*(MAP() for _ in range(n))))
INF = float('inf')
MOD = 10 ** 9 + 7

class Combination(object):

	def __init__(self, N, mod=MOD):
		(fac, finv, inv) = ([0] * (N + 1), [0] * (N + 1), [0] * (N + 1))
		fac[:2] = (1, 1)
		finv[:2] = (1, 1)
		inv[1] = 1
		for i in range(2, N + 1):
			fac[i] = fac[i - 1] * i % mod
			inv[i] = -inv[mod % i] * (mod // i) % mod
			finv[i] = finv[i - 1] * inv[i] % mod
		self.N = N
		self.MOD = mod
		self.fac = fac
		self.finv = finv
		self.inv = inv

	def __call__(self, n, k):
		return self.C(n, k)

	def C(self, n, k):
		if n < k:
			return 0
		if n < 0 or k < 0:
			return 0
		fac = self.fac
		finv = self.finv
		mod = self.MOD
		return fac[n] * (finv[k] * finv[n - k] % mod) % mod

	def P(self, n, k):
		if n < k:
			return 0
		if n < 0 or k < 0:
			return 0
		return self.fac[n] * self.finv[n - k]

def main():
	(N, M) = MAP()
	ans = 0
	cmb = Combination(M)
	sign = +1
	for i in range(N + 1):
		ans += sign * cmb.P(M - i, N - i) * cmb(N, i)
		ans %= MOD
		sign *= -1
	ans *= cmb.P(M, N)
	ans %= MOD
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
U = 5 * 10 ** 5
MOD = 10 ** 9 + 7
fact = [1] * (U + 1)
fact_inv = [1] * (U + 1)
for i in range(1, U + 1):
	fact[i] = fact[i - 1] * i % MOD
fact_inv[U] = pow(fact[U], MOD - 2, MOD)
for i in range(U, 0, -1):
	fact_inv[i - 1] = fact_inv[i] * i % MOD

def perm(n, k):
	if k < 0 or k > n:
		return 0
	z = fact[n]
	z *= fact_inv[n - k]
	z %= MOD
	return z

def comb(n, k):
	if k < 0 or k > n:
		return 0
	z = fact[n]
	z *= fact_inv[k]
	z *= fact_inv[n - k]
	z %= MOD
	return z
(n, m) = map(int, input().split())
ans = 0
for i in range(n + 1):
	temp = (-1) ** i * comb(n, i)
	temp *= perm(m, i)
	temp %= MOD
	temp *= perm(m - i, n - i) ** 2
	temp %= MOD
	ans += temp
	ans %= MOD
print(ans)


mod = 10 ** 9 + 7
(n, m) = map(int, input().split())
fac = [1] * (m + 1)
for i in range(1, m):
	fac[i + 1] = fac[i] * (i + 1) % mod

def inv(x):
	return pow(x, mod - 2, mod)

def cmb(n, k, mod):
	return fac[n] * inv(fac[k]) * inv(fac[n - k])

def per(n, k, mod):
	return fac[n] * inv(fac[n - k])
ans = 0
for k in range(n + 1):
	ans += cmb(n, k, mod) * (-1) ** k * per(m, k, mod) * per(m - k, n - k, mod) ** 2
	ans %= mod
print(ans)


mod = 10 ** 9 + 7
n = 10 ** 6
inv_t = [0] + [1]
for i in range(2, n):
	inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]
kai = [1, 1]
rev_kai = [1, inv_t[1]]
for i in range(2, n):
	kai.append(kai[-1] * i % mod)
	rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

def cmb(n, r):
	return kai[n] * rev_kai[r] * rev_kai[n - r] % mod

def prm(n, r):
	return kai[n] * rev_kai[n - r] % mod
(N, M) = map(int, input().split())

def f(x):
	e = cmb(N, x) * prm(M, x) * prm(M - x, N - x) * prm(M - x, N - x)
	e %= mod
	return e
ans = 0
for i in range(N + 1):
	g = f(i) * (-1) ** i
	ans += g
print(ans % mod)


MOD = 10 ** 9 + 7
fac = [1, 1]
inv = [0, 1]
finv = [1, 1]

def finv_init(n, m):
	for i in range(2, n + 1):
		fac.append(fac[-1] * i % m)
		inv.append(-inv[m % i] * (m // i) % m)
		finv.append(finv[-1] * inv[-1] % m)

def nPr(n, r, m):
	return fac[n] * finv[n - r] % m
(n, m) = list(map(int, input().split()))
finv_init(m, MOD)
ans = 0
for d in range(0, n + 1):
	a = nPr(m - d, n - d, MOD) * nPr(n, d, MOD) * finv[d] % MOD
	if d & 1:
		a = -a
	ans += a
	ans %= MOD
ans *= nPr(m, n, MOD)
print(ans % MOD)


import sys
readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda : readline().rstrip()
ni = lambda : int(readline().rstrip())
nm = lambda : map(int, readline().split())
nl = lambda : list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')
n_ = 5 * 10 ** 5
mod = 10 ** 9 + 7
fun = [1] * (n_ + 1)
for i in range(1, n_ + 1):
	fun[i] = fun[i - 1] * i % mod
rev = [1] * (n_ + 1)
rev[n_] = pow(fun[n_], mod - 2, mod)
for i in range(n_ - 1, 0, -1):
	rev[i] = rev[i + 1] * (i + 1) % mod

def nCr(n, r):
	if r > n:
		return 0
	return fun[n] * rev[r] % mod * rev[n - r] % mod

def modinv(x, mod):
	(a, b) = (x, mod)
	(u, v) = (1, 0)
	while b:
		t = a // b
		a -= t * b
		(a, b) = (b, a)
		u -= t * v
		(u, v) = (v, u)
	return u % mod

def solve():
	(n, m) = nm()
	s = 0
	v = 1
	for i in range(n + 1):
		s = (s + v * nCr(n, i) * nCr(m, i) * fun[i] % mod * (nCr(m - i, n - i) * fun[n - i] % mod) ** 2) % mod
		v *= -1
	print(s)
	return
solve()


(N, M) = list(map(int, input().split()))
p = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, M + 1):
	fact.append(fact[-1] * i % p)
	inv.append(-inv[p % i] * (p // i) % p)
	factinv.append(factinv[-1] * inv[-1] % p)

def cmb(n, r, p):
	if r < 0 or n < r:
		return 0
	r = min(r, n - r)
	return fact[n] * factinv[r] * factinv[n - r] % p

def pmt(n, r, p):
	if r < 0 or n < r:
		return 0
	return fact[n] * factinv[n - r] % p
C = 0
for s in range(N + 1):
	C = (C + (-1) ** s * cmb(N, s, p) * pmt(M, N, p) * pmt(M - s, N - s, p)) % p
print(C)


MOD = 10 ** 9 + 7
(n, m) = map(int, input().split())
fac = [1] * (m + 1)
fac_inv = [0] * (m + 1)
for i in range(m):
	fac[i + 1] = fac[i] * (i + 1) % MOD
fac_inv[-1] = pow(fac[-1], MOD - 2, MOD)
for i in range(m, 0, -1):
	fac_inv[i - 1] = fac_inv[i] * i % MOD

def comb(n, k):
	if k < 0 or k > n:
		return 0
	x = fac[n] * (fac_inv[k] * fac_inv[n - k]) % MOD % MOD
	return x
ans = 0
for i in range(n + 1):
	ans += (-1) ** (i % 2) * comb(n, i) * (fac[m - i] * fac_inv[m - n] % MOD) % MOD
	ans %= MOD
ans *= comb(m, n) * fac[n] % MOD
ans %= MOD
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
framod = [1]

def framod_calc(n, mod, a=1):
	for i in range(1, n + 1):
		a = a * i % mod
		framod.append(a)

def permmod(n, k, mod):
	a = framod[n]
	c = framod[n - k]
	return a * pow(c, mod - 2, mod) % mod

def combmod(n, k, mod):
	a = framod[n]
	b = framod[k]
	c = framod[n - k]
	return a * pow(b, mod - 2, mod) * pow(c, mod - 2, mod) % mod
framod_calc(m, mod)
a = combmod(m, n, mod) * framod[n]
a %= mod
b = 0
for k in range(n + 1):
	b += (-1) ** k * combmod(n, k, mod) * combmod(m - k, n - k, mod) % mod * framod[n - k] % mod
	b %= mod
print(a * b % mod)


fact = [1] * 500001
mod = 10 ** 9 + 7
for i in range(1, 500001):
	fact[i] = fact[i - 1] * i
	fact[i] %= mod

def nCr(n, r):
	global fact, mod
	if n < r:
		return 0
	return fact[n] * pow(fact[r] * fact[n - r], mod - 2, mod)
(n, m) = map(int, input().split())
mult = fact[m] * pow(fact[m - n], mod - 2, mod) % mod
a = 0
for i in range(0, n + 1):
	add = nCr(n, i) * nCr(m - i, n - i) * fact[n - i] % mod
	if i % 2 == 0:
		a += add
	else:
		a -= add
print(mult * a % mod)


(n, m) = map(int, input().split())
mod = pow(10, 9) + 7

def cmb(n, r, mod):
	if r < 0 or r > n:
		return 0
	r = min(r, n - r)
	return g1[n] * g2[r] * g2[n - r] % mod
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range(2, m + 1):
	g1.append(g1[-1] * i % mod)
	inverse.append(-inverse[mod % i] * (mod // i) % mod)
	g2.append(g2[-1] * inverse[-1] % mod)
ans_a = g1[m] * g2[m - n]
ans_b = 0
for i in range(n + 1):
	tmp = cmb(n, i, mod)
	tmp *= g1[m - i] * g2[m - n] % mod
	tmp %= mod
	ans_b += tmp * (-1) ** i
	ans_b %= mod
print(ans_a * ans_b % mod)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
factorial = [1]
inverse = [1]
for i in range(1, m + 1):
	factorial.append(factorial[-1] * i % mod)
	inverse.append(pow(factorial[-1], mod - 2, mod))

def comb(n, r, mod):
	if n < r or r < 0:
		return 0
	elif r == 0:
		return 1
	return factorial[n] * inverse[r] * inverse[n - r] % mod

def perm(n, r, mod):
	if n < r or r < 0:
		return 0
	elif r == 0:
		return 1
	return factorial[n] * inverse[n - r] % mod
ans = 0
for i in range(n + 1):
	ans += (-1) ** i * comb(n, i, mod) * perm(m, i, mod) * perm(m - i, n - i, mod) ** 2
	ans %= mod
print(ans)


mod = 10 ** 9 + 7

def mod_prepare(N):
	fac = [1, 1]
	finv = [1, 1]
	inv = [0, 1]
	for i in range(2, N + 1):
		fac.append(fac[i - 1] * i % mod)
		inv.append(mod - inv[mod % i] * (mod // i) % mod)
		finv.append(finv[i - 1] * inv[i] % mod)
	return (fac, finv)
(N, M) = map(int, input().split())
(fac, inv) = mod_prepare(M)
ans = 0
sign = 1
for k in range(N + 1):
	ans += sign * fac[M - k] * inv[k] * inv[N - k]
	sign *= -1
	ans %= mod
print(ans * fac[N] * fac[M] * inv[M - N] * inv[M - N] % mod)


(N, M) = map(int, input().split())
mod_ = 10 ** 9 + 7
nA = 1
for i in range(N):
	nA *= M - i
	nA %= mod_
dp = [0 for i in range(N + 1)]
dp[1] = M - N
if N == 1:
	print(nA * dp[N] % mod_)
else:
	dp[2] = M - N + 1 + (M - N) * (M - N)
	dp[1] %= mod_
	dp[2] %= mod_
	for i in range(3, N + 1):
		dp[i] = dp[i - 1] * (M - N + i - 1) + dp[i - 2] * (i - 1)
		dp[i] %= mod_
	print(nA * dp[N] % mod_)


def prepare(n, MOD):
	f = 1
	factorials = [1]
	for m in range(1, n + 1):
		f *= m
		f %= MOD
		factorials.append(f)
	inv = pow(f, MOD - 2, MOD)
	invs = [1] * (n + 1)
	invs[n] = inv
	for m in range(n, 1, -1):
		inv *= m
		inv %= MOD
		invs[m - 1] = inv
	return (factorials, invs)
(n, m) = map(int, input().split())
MOD = 10 ** 9 + 7
(facts, finvs) = prepare(m, MOD)
c = 1
ans = 0
base = facts[n] * facts[m] * finvs[m - n] * finvs[m - n] % MOD
for k in range(n + 1):
	tmp = base * facts[m - k] * finvs[k] * finvs[n - k] % MOD
	ans = (ans + c * tmp) % MOD
	c *= -1
print(ans)


(n, m) = map(int, input().split())
MOD = 10 ** 9 + 7
MAX = m + 1
fact = [1] * (MAX + 1)
finv = [1] * (MAX + 1)
iinv = [1] * (MAX + 1)
for i in range(2, MAX + 1):
	fact[i] = fact[i - 1] * i % MOD
	iinv[i] = MOD - iinv[MOD % i] * (MOD // i) % MOD
	finv[i] = finv[i - 1] * iinv[i] % MOD

def comb(n: int, k: int) -> int:
	if n < k or n < 0 or k < 0:
		return 0
	return fact[n] * finv[k] % MOD * finv[n - k] % MOD

def perm(n: int, k: int) -> int:
	if n < k or n < 0 or k < 0:
		return 0
	return fact[n] * finv[n - k] % MOD
ans = perm(m, n)
for i in range(1, n + 1):
	ans += (-1) ** (i % 2) * comb(n, i) * perm(m - i, n - i) % MOD
	ans %= MOD
ans *= perm(m, n)
ans %= MOD
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7

def calc(n):
	f = 1
	factorials = [1]
	for m in range(1, n + 1):
		f *= m
		f %= mod
		factorials.append(f)
	inv = pow(f, mod - 2, mod)
	invs = [1] * (n + 1)
	invs[n] = inv
	for m in range(n, 1, -1):
		inv *= m
		inv %= mod
		invs[m - 1] = inv
	return (factorials, invs)
(factorials, invs) = calc(M)
mPn = factorials[M] * invs[M - N] % mod
ans = mPn * mPn % mod
for k in range(1, N + 1):
	v = pow(-1, k - 1) * factorials[N] * factorials[M - k] * invs[N - k] * invs[k] * invs[M - N] % mod
	ans -= v * mPn
print(ans % mod)


(N, M) = list(map(int, input().split()))
p = 10 ** 9 + 7
F = [1] * (5 * 10 ** 5 + 1)
for i in range(1, 5 * 10 ** 5 + 1):
	F[i] = F[i - 1] * i % p
MAXN = 5 * 10 ** 5 + 10
MOD = p
f = [1]
for i in range(MAXN):
	f.append(f[-1] * (i + 1) % MOD)

def nCr(n, r, mod=MOD):
	return f[n] * pow(f[r], mod - 2, mod) * pow(f[n - r], mod - 2, mod) % mod
out = 0
for K in range(N + 1):
	A = nCr(M, K, p) * F[K]
	B = nCr(M - K, N - K, p) * F[N - K]
	C = (-1) ** K
	out = (out + nCr(N, K, p) * A * B * B * C) % p
print(out)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
factorial = [1]
for i in range(1, M + 1):
	factorial.append(factorial[-1] * i % MOD)
inv_factorial = [-1] * (M + 1)
inv_factorial[-1] = pow(factorial[-1], MOD - 2, MOD)
for i in reversed(range(M)):
	inv_factorial[i] = inv_factorial[i + 1] * (i + 1) % MOD
ans = 0
for k in range(N + 1):
	ans += factorial[N] * inv_factorial[k] * inv_factorial[N - k] * (-1) ** (k % 2) * factorial[M] * inv_factorial[M - k] * pow(factorial[M - k] * inv_factorial[M - N], 2, MOD)
	ans %= MOD
print(ans)


from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque, Counter
from operator import mul
from functools import reduce
N = 10 ** 6
mod = 10 ** 9 + 7
inv_t = [0] + [1]
for i in range(2, N):
	inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]
kai = [1, 1]
rev_kai = [1, inv_t[1]]
for i in range(2, N):
	kai.append(kai[-1] * i % mod)
	rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

def cmb(n, r):
	return kai[n] * rev_kai[r] * rev_kai[n - r] % mod

def prm(n, r):
	return kai[n] * rev_kai[n - r] % mod
(n, m) = list(map(int, input().split()))
a = 0
for k in range(n + 1):
	a += prm(m, k) * prm(m - k, n - k) ** 2 * (-1) ** k * cmb(n, k) % mod
print(a % mod)


MOD = 10 ** 9 + 7
fac = [1] + [0] * (5 * 10 ** 5)
rev_fac = [0] * (5 * 10 ** 5 + 1)
for i in range(1, len(fac)):
	fac[i] = fac[i - 1] * i % MOD
rev_fac[-1] = pow(fac[-1], MOD - 2, MOD)
for i in range(len(rev_fac) - 2, -1, -1):
	rev_fac[i] = rev_fac[i + 1] * (i + 1) % MOD
(N, M) = map(int, input().split())
ans = 0
for k in range(N + 1):
	ans = (ans + pow(-1, k) * fac[N] * rev_fac[k] * rev_fac[N - k] * fac[M] * rev_fac[M - k] * (fac[M - k] * rev_fac[M - N]) ** 2) % MOD
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]

def comb(n, r):
	return fac[n] * (finv[r] * finv[n - r] % mod) % mod

def perm(n, r):
	return fac[n] * finv[n - r] % mod
for i in range(2, M + 1):
	fac.append(fac[-1] * i % mod)
	inv.append(mod - inv[mod % i] * (mod // i) % mod)
	finv.append(finv[-1] * inv[-1] % mod)
ans = 0
for K in range(N + 1):
	ans += comb(N, K) * (-1) ** (K % 2) % mod * perm(M, K) % mod * pow(perm(M - K, N - K), 2, mod)
	ans %= mod
print(ans)


MOD = 10 ** 9 + 7

def init(n=5 * 10 ** 5):
	fact = [1] * (n + 1)
	for i in range(2, n + 1):
		fact[i] = i * fact[i - 1] % MOD
	inv = [1] * (n + 1)
	inv[n] = pow(fact[n], MOD - 2, MOD)
	for i in range(n, 0, -1):
		inv[i - 1] = i * inv[i] % MOD
	return (fact, inv)

def nCr(n, r):
	r = min(r, n - r)
	return fact[n] * (inv[n - r] * inv[r] % MOD) % MOD

def nPr(n, r):
	return fact[n] * inv[n - r] % MOD
(N, M) = map(int, input().split())
(fact, inv) = init()
res = 0
sgn = 1
for i in range(N + 1):
	res += sgn * nPr(M - i, N - i) * nCr(N, i)
	res %= MOD
	sgn *= -1
print(nPr(M, N) * res % MOD)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
(n, m) = map(int, readline().split())
MOD = 10 ** 9 + 7
res = [0] * (n + 1)
res[0] = 1
res[1] = m - n
for i in range(2, n + 1):
	res[i] = (m - n + i - 1) * res[i - 1] + (i - 1) * res[i - 2]
	res[i] %= MOD
ans = res[n]
for i in range(m, m - n, -1):
	ans *= i
	ans %= MOD
print(ans)


MOD = 10 ** 9 + 7
(n, m) = map(int, input().split())
fac = [1] * (m + 1)
for i in range(m):
	fac[i + 1] = fac[i] * (i + 1) % MOD

def comb(n, k):
	if n < 0 or n < k or k < 0:
		return 0
	return fac[n] * pow(fac[n - k], MOD - 2, MOD) * pow(fac[k], MOD - 2, MOD) % MOD
ans = 0
t = 0
cnt = 0
a = 1
for j in range(n + 1):
	if j % 2 == 0:
		ans += comb(m - j, n - j) * fac[n - j] * comb(n, j)
	else:
		ans -= comb(m - j, n - j) * fac[n - j] * comb(n, j)
print(ans * comb(m, n) * fac[n] % MOD)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
ans = 1
for i in range(N):
	ans *= M - i
	ans %= MOD
(d2, d1) = (1, M - N)
d0 = d1
for i in range(2, N + 1):
	d0 = (i - 1) * d2 % MOD
	d0 += (M - N + i - 1) * d1
	d0 %= MOD
	(d2, d1) = (d1, d0)
print(ans * d0 % MOD)


SIZE = 5 * 10 ** 5 + 1
MOD = 10 ** 9 + 7
SIZE += 1
inv = [0] * SIZE
fac = [0] * SIZE
finv = [0] * SIZE
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2, SIZE):
	inv[i] = MOD - MOD // i * inv[MOD % i] % MOD
	fac[i] = fac[i - 1] * i % MOD
	finv[i] = finv[i - 1] * inv[i] % MOD

def choose(n, r):
	if 0 <= r <= n:
		return fac[n] * finv[r] % MOD * finv[n - r] % MOD
	else:
		return 0

def chofuku(n, r):
	return choose(n + r - 1, r)

def narabekae(n, r):
	return fac[n] * finv[n - r] % MOD
(N, M) = list(map(int, input().split()))
ans = 0
for k in range(N + 1):
	ans += choose(N, k) * narabekae(M - k, N - k) * (-1 if k % 2 == 1 else 1)
	ans %= MOD
ans *= narabekae(M, N)
print(ans % MOD)


(n, m) = map(int, input().split(' '))
mod = int(1000000000.0 + 7)
l = [1] * (m + 1)
for i in range(1, m + 1):
	l[i] = l[i - 1] * i % mod
l2 = [1] * (m + 1)
l2[m] = pow(l[m], mod - 2, mod)
for i in range(m - 1, 0, -1):
	l2[i] = l2[i + 1] * (i + 1) % mod
s = 0
for k in range(n + 1):
	n_choose_k = l[n] * l2[k] * l2[n - k] % mod
	m_perm_k = l[m] * l2[m - k] % mod
	mk_perm_nk = l[m - k] * l2[m - n] % mod
	s += (-1) ** k * n_choose_k * m_perm_k * mk_perm_nk ** 2 % mod
print(s % mod)


from functools import lru_cache

class Combination:

	def __init__(self, size):
		self.size = size + 2
		self.fact = [1, 1] + [0] * size
		self.factInv = [1, 1] + [0] * size
		self.inv = [0, 1] + [0] * size
		for i in range(2, self.size):
			self.fact[i] = self.fact[i - 1] * i % MOD
			self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
			self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

	def npr(self, n, r):
		if n < r or n < 0 or r < 0:
			return 0
		return self.fact[n] * self.factInv[n - r] % MOD

	def ncr(self, n, r):
		if n < r or n < 0 or r < 0:
			return 0
		return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

	def nhr(self, n, r):
		return self.ncr(n + r - 1, n - 1)
(N, M) = list(map(int, input().split()))
MOD = 10 ** 9 + 7
comb = Combination(M + 100)
ans = 0
for k in range(N + 1):
	ans += comb.ncr(N, k) * (-1) ** k * comb.npr(M, k) * comb.npr(M - k, N - k) ** 2
	ans %= MOD
print(ans)


def main():
	mod = 10 ** 9 + 7
	fact = [1, 1]
	for i in range(2, 5 * 10 ** 5 + 1):
		fact.append(fact[-1] * i % mod)

	def nPr(n, r, mod=10 ** 9 + 7):
		return pow(fact[n - r], mod - 2, mod) * fact[n] % mod
	(n, m) = map(int, input().split())
	if m == 1:
		print(0)
		return
	ans = nPr(m, n)
	if m - n == 1:
		a0 = 1
	else:
		a0 = 0
	a1 = 1
	for i in range(m - n - 1, m - 1):
		(a1, a0) = (((i + 1) * a1 + (i - (m - n - 1)) * a0) % mod, a1)
	print(a1 * ans % mod)
main()


def main():
	(N, M) = map(int, input().split())
	P = 10 ** 9 + 7
	mPn = M
	for i in range(M - N + 1, M):
		mPn = mPn * i % P
	num_b = 0
	nCk = 1
	m_kPn_k = mPn
	for k in range(1, N + 1):
		nCk = nCk * (N - k + 1) * pow(k, P - 2, P) % P
		m_kPn_k = m_kPn_k * pow(M - k + 1, P - 2, P) % P
		num_b = (num_b + (-1) ** (k - 1) * nCk * m_kPn_k) % P
	print((mPn ** 2 - mPn * num_b) % P)
main()


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
max = 5 * 10 ** 5 + 100
(fac, finv, inv) = ([0] * max, [0] * max, [0] * max)

def comInit(max):
	fac[0] = fac[1] = 1
	finv[0] = finv[1] = 1
	inv[1] = 1
	for i in range(2, max):
		fac[i] = fac[i - 1] * i % mod
		inv[i] = mod - inv[mod % i] * (mod // i) % mod
		finv[i] = finv[i - 1] * inv[i] % mod
comInit(max)

def com(n, k):
	if n < k:
		return 0
	if (n < 0) | (k < 0):
		return 0
	return fac[n] * (finv[k] * finv[n - k] % mod) % mod
cnt = [0] * (n + 1)
tot = 0
for i in range(n, -1, -1):
	cnt[i] = com(n, i) * fac[n - i] * com(m - i, n - i)
	cnt[i] %= mod
	tot = cnt[i] - tot
	tot %= mod
ans = tot * fac[m] * finv[m - n]
ans %= mod
print(ans)


import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
fac = [1] * (5 * 10 ** 5 + 1)
for i in range(1, 5 * 10 ** 5 + 1):
	fac[i] = fac[i - 1] * i % MOD

def inv(x):
	return pow(x, MOD - 2, MOD)

def nPr(n, r):
	return fac[n] * inv(fac[n - r]) % MOD

def nCr(n, r):
	return fac[n] * inv(fac[n - r]) * inv(fac[r]) % MOD

def main():
	(N, M) = map(int, input().split())
	hojo = 0
	for i in range(1, N + 1):
		hojo += (-1) ** i * nCr(N, i) * nPr(M, i) * nPr(M - i, N - i) ** 2
		hojo %= MOD
	print((nPr(M, N) ** 2 + hojo) % MOD)
main()


import sys
input = sys.stdin.readline
(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
FACT = [1]
for i in range(1, 5 * 10 ** 5 + 1):
	FACT.append(FACT[-1] * i % mod)
FACT_INV = [pow(FACT[-1], mod - 2, mod)]
for i in range(5 * 10 ** 5, 0, -1):
	FACT_INV.append(FACT_INV[-1] * i % mod)
FACT_INV.reverse()

def Combi(a, b):
	if 0 <= b <= a:
		return FACT[a] * FACT_INV[b] * FACT_INV[a - b] % mod
	else:
		return 0
ANS = 0
for i in range(N + 1):
	if i % 2 == 0:
		ANS = (ANS + Combi(N, i) * FACT[M - i] * FACT_INV[M - N]) % mod
	else:
		ANS = (ANS - Combi(N, i) * FACT[M - i] * FACT_INV[M - N]) % mod
print(ANS * FACT[M] * FACT_INV[M - N] % mod)


(n, m) = list(map(int, input().split()))
mod = 10 ** 9 + 7
N = 10 ** 6
inv_t = [0] + [1]
for i in range(2, N):
	inv_t += [inv_t[mod % i] * (mod - int(mod / i)) % mod]
kai = [1, 1]
rev_kai = [1, inv_t[1]]
for i in range(2, N):
	kai.append(kai[-1] * i % mod)
	rev_kai.append(rev_kai[-1] * inv_t[i] % mod)

def cmb(n, r):
	return kai[n] * rev_kai[r] * rev_kai[n - r] % mod

def prm(n, r):
	return kai[n] * rev_kai[n - r] % mod
ans = 0
tmp = 1
for k in range(n + 1):
	ans += tmp * cmb(n, k) * prm(m - k, n - k) % mod
	ans %= mod
	tmp *= -1
ans *= prm(m, n)
ans %= mod
print(ans)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
factorial = [1, 1]
inverse = [1, 1]
invere_base = [0, 1]
for i in range(2, N + M + 1):
	factorial.append(factorial[-1] * i % MOD)
	invere_base.append(-invere_base[MOD % i] * (MOD // i) % MOD)
	inverse.append(inverse[-1] * invere_base[-1] % MOD)

def nCr(n, r):
	if not 0 <= r <= n:
		return 0
	return factorial[n] * inverse[r] * inverse[n - r] % MOD

def nPr(n, r):
	if not 0 <= r <= n:
		return 0
	return nCr(n, r) * factorial[r] % MOD
ans = pow(nCr(M, N) * factorial[N], 2, MOD)
for r in range(1, N + 1):
	p = nCr(M, r) * nPr(N, r) * pow(nPr(M - r, N - r), 2, MOD) % MOD
	p *= (-1) ** (r % 2 ^ 1)
	ans -= p
	ans %= MOD
print(ans)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
n = max(N, M)
fac = [1] * (n + 1)
rev = [1] * (n + 1)
for i in range(1, n + 1):
	fac[i] = i * fac[i - 1] % MOD
	rev[i] = pow(fac[i], MOD - 2, MOD)
comb = lambda a, b: fac[a] * rev[a - b] * rev[b] % MOD
perm = lambda a, b: fac[a] * rev[a - b] % MOD
ans = 0
for k in range(N + 1):
	ans += comb(N, k) * (-1) ** k * perm(M, k) * perm(M - k, N - k) ** 2
	ans %= MOD
print(ans)


(N, M) = map(int, input().split())
mod = int(1000000000.0 + 7)
kai = [1 for _ in range(N + M + 1)]
for i in range(2, N + M + 1):
	kai[i] = kai[i - 1] * i % mod
ap = 0
for i in range(N + 1):
	sp = kai[M] * pow(kai[M - i], mod - 2, mod) % mod
	restp = kai[M - i] * pow(kai[M - N], mod - 2, mod) % mod
	chose = kai[N] * pow(kai[N - i], mod - 2, mod) % mod * pow(kai[i], mod - 2, mod) % mod
	sign = (-1) ** i
	ap = (ap + sign * sp % mod * (chose * restp ** 2) % mod) % mod
print(ap % mod)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
for i in range(2, M + 1):
	fact.append(fact[-1] * i % mod)
	inv.append(-inv[mod % i] * (mod // i) % mod)
	factinv.append(factinv[-1] * inv[-1] % mod)
ans = 0
for k in range(N + 1):
	if k % 2 == 0:
		ans = (ans + factinv[N - k] * factinv[k] % mod * fact[M - k] % mod) % mod
	else:
		ans = (ans - factinv[N - k] * factinv[k] % mod * fact[M - k] % mod) % mod
ans = ans * fact[N] % mod * fact[M] % mod * factinv[M - N] % mod * factinv[M - N] % mod
print(ans)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
fac = [1]
for i in range(1, N + M + 10):
	fac.append(fac[-1] * i % mod)
inv = [pow(fac[-1], mod - 2, mod)]
for i in range(N + M + 9, 0, -1):
	inv.append(inv[-1] * i % mod)
inv = inv[::-1]

def comb(n, r):
	if n >= r and r >= 0:
		return fac[n] * inv[n - r] * inv[r] % mod
	return 1
ret = 0
for i in range(N + 1):
	if i % 2 == 1:
		sign = -1
	else:
		sign = 1
	x = sign * comb(M, i) * comb(N, i) * fac[i] % mod
	x *= fac[M - i] ** 2 * inv[M - N] ** 2 % mod
	ret += x % mod
	ret %= mod
print(ret)


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7

def fact_list(a):
	ans = [1]
	for i in range(1, a + 1):
		ans.append(ans[-1] * i % mod)
	return ans
factlist = fact_list(M)

def comb_mod(a, b):
	return factlist[a] * pow(factlist[b], mod - 2, mod) * pow(factlist[a - b], mod - 2, mod) % mod

def permu_mod(a, b):
	return factlist[a] * pow(factlist[a - b], mod - 2, mod) % mod
ans = 0
for i in range(N + 1):
	ans += comb_mod(N, i) * (-1) ** i * permu_mod(M, i) * permu_mod(M - i, N - i) ** 2
	ans = ans % mod
print(ans)


import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
import numpy as np
from collections import deque

def generate_inv(n, mod):
	ret = [0, 1]
	for i in range(2, n + 1):
		next = -ret[mod % i] * (mod // i)
		next %= mod
		ret.append(next)
	return ret

def generate_comb_m_i(M, N, inv, mod):
	ret = [1, M]
	for i in range(2, N + 1):
		tmp = ret[-1] * (M - (i - 1)) * inv[i] % mod
		ret.append(tmp)
	return ret

def generate_p_N_i(N, mod):
	ret = [1]
	for i in range(1, N + 1):
		tmp = ret[-1] * (N - (i - 1)) % mod
		ret.append(tmp)
	return ret

def generate_p_mi_ni(M, N, mod):
	ret = deque([1])
	cache = deque([1])
	for i in range(1, N + 1):
		tmp = cache[0] * (M - N + i) % mod
		cache.appendleft(tmp)
		ret.appendleft(tmp ** 2 % mod)
	return ret

def run():
	(N, M) = map(int, input().split())
	mod = 10 ** 9 + 7
	inv = generate_inv(M, mod)
	comb_m_i = generate_comb_m_i(M, N, inv, mod)
	p_N_i = generate_p_N_i(N, mod)
	p_mi_ni = generate_p_mi_ni(M, N, mod)
	ret = p_mi_ni[0]
	arr01 = [-1, 1] * (N // 2 + 1)
	arr01 = arr01[:N + 1]
	comb_m_i = np.array(comb_m_i, dtype=np.int64)
	p_N_i = np.array(p_N_i, dtype=np.int64)
	p_mi_ni = np.array(p_mi_ni, dtype=np.int64)
	arr01 = np.array(arr01, dtype=np.int64)
	tmp = comb_m_i * p_N_i % mod
	tmp = tmp * p_mi_ni % mod
	tmp = tmp * arr01 % mod
	tmp = tmp[1:]
	sub = 0
	for val in tmp:
		sub += val
		sub %= mod
	ret -= sub
	ret %= mod
	print(ret)

def __starting_point():
	run()
__starting_point()


(n, m) = map(int, input().split())
d = [1] * (n + 1)
for i in range(n):
	d[i + 1] = (m - i) * ((m - n + i) * d[i] + i * d[i - 1] * (m - i + 1)) % (10 ** 9 + 7)
print(d[-1])


class Combination:

	def __init__(self, n_max, mod=10 ** 9 + 7):
		self.mod = mod
		f = 1
		self.fac = fac = [f]
		for i in range(1, n_max + 1):
			f = f * i % mod
			fac.append(f)
		f = pow(f, mod - 2, mod)
		self.facinv = facinv = [f]
		for i in range(n_max, 0, -1):
			f = f * i % mod
			facinv.append(f)
		facinv.reverse()

	def __call__(self, n, r):
		return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

	def nCr(self, n, r):
		if not 0 <= r <= n:
			return 0
		return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n - r] % self.mod

	def nPr(self, n, r):
		if not 0 <= r <= n:
			return 0
		return self.fac[n] * self.facinv[n - r] % self.mod
MOD = 10 ** 9 + 7

def resolve():
	(N, M) = list(map(int, input().split()))
	cmb = Combination(5 * 10 ** 5)
	ans = 0
	for p in range(N + 1):
		ans += (-1) ** p * cmb.nCr(N, p) * cmb.nPr(M - p, N - p)
		ans %= MOD
	ans *= cmb.nPr(M, N)
	ans %= MOD
	print(ans)

def __starting_point():
	resolve()
__starting_point()


import sys
import math
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline

def main():
	(N, M) = [int(x) for x in input().split()]
	MOD = 10 ** 9 + 7
	fact = [1, 1]
	factinv = [1, 1]
	inv = [0, 1]

	def cmb(n, k, p):
		if k < 0 or n < k:
			return 0
		r = min(k, n - k)
		return fact[n] * factinv[k] * factinv[n - k] % p
	for i in range(2, 10 ** 6 + 1):
		fact.append(fact[-1] * i % MOD)
		inv.append(-inv[MOD % i] * (MOD // i) % MOD)
		factinv.append(factinv[-1] * inv[-1] % MOD)
	X = fact[M] * factinv[M - N]
	ans = 0
	for i in range(N + 1):
		c = fact[M - i] * factinv[M - i - (N - i)]
		if i % 2 == 0:
			ans += X * c * cmb(N, i, MOD)
		else:
			ans -= X * c * cmb(N, i, MOD)
		ans %= MOD
	print(ans % MOD)

def __starting_point():
	main()
__starting_point()


MOD = 10 ** 9 + 7
MOD_t_MAX = 5 * 10 ** 5 + 10
fac = [None] * MOD_t_MAX
finv = [None] * MOD_t_MAX
inv = [None] * MOD_t_MAX

def MOD_COM_init():
	fac[0] = fac[1] = 1
	finv[0] = finv[1] = 1
	inv[1] = 1
	for i in range(2, MOD_t_MAX):
		fac[i] = fac[i - 1] * i % MOD
		inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
		finv[i] = finv[i - 1] * inv[i] % MOD

def MOD_COM(n, k):
	if n < k:
		return 0
	if n < 0 or k < 0:
		return 0
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

def MOD_perm(n, r):
	rtn = 1
	for _ in range(r):
		rtn *= n
		rtn %= MOD
		n -= 1
	return rtn

def main():
	(n, m) = map(int, input().split())
	ans = 0
	perm = MOD_perm(m, n)
	MOD_COM_init()
	for i in range(n + 1):
		tmp = perm * MOD_COM(n, i) % MOD
		ans += (-1) ** i * tmp
		ans %= MOD
		if i < n:
			perm *= inv[m - i]
			perm %= MOD
	print(ans * MOD_perm(m, n) % MOD)

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
g1 = [1, 1]
g2 = [0, 1]
for i in range(2, m + 1):
	g2.append(-g2[mod % i] * (mod // i) % mod)
	g1.append(g1[-1] * g2[-1] % mod)
g3 = [1, m - n + 1]
for i in range(n - 1):
	g3.append(g3[-1] * (m - n + i + 2) % mod)
x = 1
for i in range(2, n + 1):
	x = x * i % mod
k = 0
for i in range(1, n + 1):
	k = (k + (-1) ** i * x * g1[i] * g1[n - i] * g3[n - i]) % mod
k = (k + g3[n]) % mod
k = k * g3[n] % mod
print(k)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
f = [1]
for i in range(1, max(m, n) + 1):
	f.append(f[i - 1] * i % mod)

def comb(n, r):
	return f[n] * pow(f[r], mod - 2, mod) * pow(f[n - r], mod - 2, mod) % mod

def pern(n, r):
	return f[n] * pow(f[n - r], mod - 2, mod) % mod
a = [0] * (n + 1)
a[n] = 1
for i in range(n - 1, -1, -1):
	a[i] = (comb(n, n - i) * pern(m - i, n - i) - a[i + 1]) % mod
print(a[0] * pern(m, n) % mod)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
d = m - n
p = 1
for i in range(n):
	p *= m - i
	p %= mod
dp = [0, 1]
for i in range(n):
	tmp = dp[-2] * i + dp[-1] * (i + d)
	tmp %= mod
	dp.append(tmp)
p *= dp[-1]
p %= mod
print(p)


mod = 10 ** 9 + 7

def comb(n, r):
	if n < r:
		return 0
	if n < 0 or r < 0:
		return 0
	return fa[n] * fi[r] % mod * fi[n - r] % mod

def per(n, r):
	if n < r:
		return 0
	if n < 0 or r < 0:
		return 0
	return fa[n] * fi[n - r] % mod
(n, m) = map(int, input().split())
fa = [1] * (m + 1)
fi = [1] * (m + 1)
for i in range(1, m + 1):
	fa[i] = fa[i - 1] * i % mod
	fi[i] = pow(fa[i], mod - 2, mod)
ans = 0
for i in range(n + 1):
	if i % 2 == 0:
		ans += comb(n, i) * per(m, i) % mod * per(m - i, n - i) ** 2 % mod
		ans %= mod
	else:
		ans += comb(n, i) * -1 * per(m, i) % mod * per(m - i, n - i) ** 2 % mod
		ans % mod
print(ans % mod)


def pre_c(n, mod):
	f = [0] * (n + 1)
	g = [0] * (n + 1)
	for i in range(n + 1):
		if i == 0:
			f[i] = 1
			g[i] = 1
		else:
			f[i] = f[i - 1] * i % mod
			g[i] = pow(f[i], mod - 2, mod)
	return (f, g)
(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
(f, g) = pre_c(m, mod)
res = 0
for i in range(n + 1):
	C = f[n] * g[i] * g[n - i]
	P1 = f[m] * g[m - i]
	P2 = f[m - i] * g[m - n]
	temp = C * P1 * P2 ** 2 * pow(-1, i)
	res = (res + temp % mod) % mod
print(res % mod)


(N, M) = map(int, input().split())
MOD = 10 ** 9 + 7
fac = [1, 1]
inv = [0, 1]
finv = [1, 1]
for i in range(2, M + 1):
	fac.append(fac[-1] * i % MOD)
	inv.append(MOD - inv[MOD % i] * (MOD // i) % MOD)
	finv.append(finv[-1] * inv[-1] % MOD)
fixed = fac[N] * fac[M] * pow(finv[M - N], 2, MOD) % MOD

def f(k):
	return fac[M - k] * finv[k] * finv[N - k] % MOD
ans = 0
for i in range(N + 1):
	ans = (ans + fixed * f(i) * pow(-1, i)) % MOD
print(ans)


MOD = 10 ** 9 + 7
MAX = int(500000.0)

def div(a, b):
	return a * pow(b, MOD - 2, MOD) % MOD
FACT = [1] * (MAX + 1)
for i in range(1, MAX + 1):
	FACT[i] = i * FACT[i - 1] % MOD
INV = [1] * (MAX + 1)
INV[MAX] = div(1, FACT[MAX])
for i in range(MAX, 0, -1):
	INV[i - 1] = INV[i] * i % MOD

def combination(m, n):
	return FACT[m] * INV[n] * INV[m - n] % MOD

def permutation(m, n):
	return FACT[m] * INV[m - n] % MOD

def main():
	(N, M) = map(int, input().split())
	ans = 0
	for i in range(N + 1):
		tmp = combination(N, i) * permutation(M, i) * permutation(M - i, N - i) ** 2 % MOD
		ans = (ans + (-1) ** i * tmp) % MOD
	print(ans)
main()


(N, M) = map(int, input().split())
mod = 10 ** 9 + 7
Fact = [0 for i in range(M + 1)]
Finv = [0 for i in range(M + 1)]
Fact[0] = 1

def inv(x):
	return pow(x, mod - 2, mod)
for i in range(M):
	Fact[i + 1] = Fact[i] * (i + 1)
	Fact[i + 1] %= mod
Finv[M] = inv(Fact[M])
for i in range(M - 1, -1, -1):
	Finv[i] = (i + 1) * Finv[i + 1]
	Finv[i] %= mod

def P(n, k):
	return Fact[n] * Finv[n - k] % mod

def C(n, k):
	return Fact[n] * Finv[n - k] * Finv[k] % mod
ans = 0
for k in range(N + 1):
	if k % 2 == 0:
		ans += C(N, k) * P(M - k, N - k)
	else:
		ans -= C(N, k) * P(M - k, N - k)
	ans %= mod
print(ans * P(M, N) % mod)


MAX_NUM = 5 * 10 ** 5 + 1
MOD = 10 ** 9 + 7
fac = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv = [0 for _ in range(MAX_NUM)]
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2, MAX_NUM):
	fac[i] = fac[i - 1] * i % MOD
	inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
	finv[i] = finv[i - 1] * inv[i] % MOD

def comb(n, k):
	if n < k:
		return 0
	if n < 0 or k < 0:
		return 0
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

def perm(n, k):
	if n < k:
		return 0
	if n < 0 or k < 0:
		return 0
	return fac[n] * finv[n - k] % MOD
(N, M) = map(int, input().split())
ans = 0
for K in range(N + 1):
	term = comb(N, K) * perm(M, K)
	term *= perm(M - K, N - K) ** 2
	term %= MOD
	if K % 2 == 0:
		ans = (ans + term) % MOD
	else:
		ans = (ans - term) % MOD
print(ans)


import sys
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

def main():
	(n, m) = na()
	mod = 1000000007
	ans = 0
	pm = 1
	nCk = 1
	tmp = 1
	for i in range(m - n + 1, m + 1):
		tmp *= i * i
		tmp %= mod
	for k in range(n + 1):
		ans += pm * nCk * tmp
		ans %= mod
		pm *= -1
		nCk *= (n - k) * pow(k + 1, mod - 2, mod)
		nCk %= mod
		tmp *= pow(m - k, mod - 2, mod)
		tmp %= mod
	print(ans)
	return
main()


def factorials_with_inv(k, mod):
	fac = [1] * (k + 1)
	inv = [1] * (k + 1)
	t = 1
	for i in range(1, k + 1):
		t *= i
		t %= mod
		fac[i] = t
	t = pow(t, -1, mod)
	for i in reversed(range(1, k + 1)):
		inv[i] = t
		t *= i
		t %= mod
	return (fac, inv)
MOD = 10 ** 9 + 7

def solve(N, M):
	(fac, inv) = factorials_with_inv(max(N, M), MOD)
	res = 0
	sign = True
	for k in range(N + 1):
		t = fac[M - k] * inv[k] * inv[N - k]
		if sign:
			res += t
		else:
			res -= t
		res %= MOD
		sign = not sign
	res *= fac[N] * fac[M] * inv[M - N] ** 2
	res %= MOD
	return res
from itertools import permutations

def naive(N, M):
	res = 0
	for A in permutations(range(M), N):
		for B in permutations(range(M), N):
			res += all((a != b for (a, b) in zip(A, B)))
	return res % MOD

def __starting_point():
	(N, M) = map(int, input().split())
	print(solve(N, M))
__starting_point()


MOD = 1000000007

class mint:

	def __init__(self, x=0):
		self.x = x % MOD

	def __str__(self):
		return str(self.x)
	__repr__ = __str__

	def __add__(self, other):
		return mint(self.x + other.x) if isinstance(other, mint) else mint(self.x + other)

	def __sub__(self, other):
		return mint(self.x - other.x) if isinstance(other, mint) else mint(self.x - other)

	def __mul__(self, other):
		return mint(self.x * other.x) if isinstance(other, mint) else mint(self.x * other)

	def __truediv__(self, other):
		return mint(self.x * pow(other.x, MOD - 2, MOD)) if isinstance(other, mint) else mint(self.x * pow(other, MOD - 2, MOD))

	def __pow__(self, other):
		return mint(pow(self.x, other.x, MOD)) if isinstance(other, mint) else mint(pow(self.x, other, MOD))
	__radd__ = __add__

	def __rsub__(self, other):
		return mint(other.x - self.x) if isinstance(other, mint) else mint(other - self.x)
	__rmul__ = __mul__

	def __rtruediv__(self, other):
		return mint(other.x * pow(self.x, MOD - 2, MOD)) if isinstance(other, mint) else mint(other * pow(self.x, MOD - 2, MOD))

	def __rpow__(self, other):
		return mint(pow(other.x, self.x, MOD)) if isinstance(other, mint) else mint(pow(other, self.x, MOD))
MAX = 1000005
fac = [1, 1]
finv = [1, 1]
inv = [0, 1]

def COMinit():
	for i in range(2, MAX):
		fac.append(fac[-1] * i % MOD)
		inv.append(-inv[MOD % i] * (MOD // i) % MOD)
		finv.append(finv[-1] * inv[-1] % MOD)

def COM(n, k):
	if n < k:
		return 0
	if n < 0 or k < 0:
		return 0
	return fac[n] * finv[k] * finv[n - k] % MOD

def P(n, k):
	return fac[n] * finv[n - k] % MOD
(N, M) = map(int, input().split())
COMinit()
res = mint(0)
now = mint()
for i in range(N + 1):
	now = COM(N, i)
	now *= P(M - i, N - i)
	if i % 2 == 1:
		now = -1 * now
	res += now
res *= P(M, N)
print(res)


def cmb(n, r, p):
	if r < 0 or n < r:
		return 0
	r = min(r, n - r)
	return facto[n] * factoinv[r] * factoinv[n - r] % p
p = 10 ** 9 + 7
(n, m) = map(int, input().split())
facto = [1, 1]
factoinv = [1, 1]
inv = [0, 1]
for i in range(2, m + 1):
	facto.append(facto[-1] * i % p)
	inv.append(-inv[p % i] * (p // i) % p)
	factoinv.append(factoinv[-1] * inv[-1] % p)
if n == m:
	kosuub = 0
	for i in range(n + 1):
		kosuub += facto[n - i] * cmb(n, i, p) * (-1) ** i % p
	kosuu = kosuub * facto[n] % p
	print(kosuu)
else:
	kosuub = 0
	for i in range(n + 1):
		kosuub += cmb(m - i, n - i, p) * facto[n - i] * cmb(n, i, p) * (-1) ** i % p
	kosuu = kosuub * cmb(m, n, p) * facto[n] % p
	print(kosuu)


MOD = 10 ** 9 + 7
(N, M) = map(int, input().split())
num = 500010
fact = [1] * (num + 1)
ifact = [1] * (num + 1)
for i in range(1, num + 1):
	fact[i] = fact[i - 1] * i % MOD
ifact[-1] = pow(fact[-1], MOD - 2, MOD)
for i in range(1, num + 1)[::-1]:
	ifact[i - 1] = ifact[i] * i % MOD

def nPr(n, r):
	if r > n:
		return 0
	return fact[n] * ifact[n - r] % MOD

def nCr(n, r):
	if r > n:
		return 0
	return fact[n] * ifact[r] * ifact[n - r] % MOD
ans = nPr(M, N) ** 2
sign = -1
for n in range(1, N + 1):
	t = nPr(M, n) * nCr(N, n) * nPr(M - n, N - n) ** 2
	ans += sign * t
	sign *= -1
	ans %= MOD
print(ans)


(n, m) = map(int, input().split())
mod = 10 ** 9 + 7
fac = [1] * (m + 1)
for i in range(m):
	fac[i + 1] = fac[i] * (i + 1) % mod

def modinv(a):
	b = mod
	u = 1
	v = 0
	while b:
		t = a // b
		a -= t * b
		(a, b) = (b, a)
		u -= t * v
		(u, v) = (v, u)
	u %= mod
	if u < 0:
		u += m
	return u
ans = fac[m] * fac[n] % mod
tmp = 0
for k in range(n + 1):
	tmp2 = fac[m - k]
	tmp2 = tmp2 * modinv(fac[n - k]) % mod
	tmp2 = tmp2 * modinv(fac[k]) % mod
	if k % 2 == 0:
		tmp = (tmp + tmp2) % mod
	else:
		tmp = (tmp - tmp2) % mod
ans = ans * tmp % mod
inv = modinv(fac[m - n])
ans = ans * inv % mod
ans = ans * inv % mod
print(ans)


(N, M) = list(map(int, input().split()))
mod = 1000000007
F = [1]
f = 1
for i in range(1, M + 1):
	f *= i
	f %= mod
	F.append(f)
ans = 0
for k in range(N + 1):
	a = F[M - k]
	b = F[M - N] * F[N - k] * F[k]
	a *= pow(b, mod - 2, mod)
	if k % 2 == 0:
		ans += a
	else:
		ans += mod - a
	ans %= mod
ans *= F[N]
ans %= mod
for i in range(N):
	ans *= M - i
	ans %= mod
print(ans)