Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def permutation(n, x, mod=10**9+7):
    # nPx
    # 順列
    # ex) permutaion(5, 2) = 20
    tmp = 1
    for i in range(n, n-x, -1):
        tmp = (tmp * i) % mod
    return tmp

def power_func(a,b,p=10**9+7):
  """a^b mod p を求める"""
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p ))%p

n,v=map(int,input().split())
m=10**9+7
perm1=deque([1])
perm2=deque([permutation(v,n)])
comb=deque([1])
ones=deque([1])
acc=((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
for k in range(n):
  #nCk+1=nCk*(n-k/k+1)
  comb.append((comb.pop()*(n-k)*power_func(k+1,m-2))%m)
  #vPk+1=vPk*(v-k)
  perm1.append((perm1.pop()*(v-k))%m)
  #v-(k+1)Pn-(k+1)=v-kPn-k/(v-k)
  perm2.append((perm2.pop()*power_func(v-k,m-2))%m)
  #(-1)^k+1=(-1)^k*(-1)
  ones.append(ones.pop()*(-1))
  acc+=ones[-1]*((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
print(acc%m)


Fixed submission:
from collections import deque
#from functools import lru_cache

def permutation(n, x, mod=10**9+7):
    # nPx
    # 順列
    # ex) permutaion(5, 2) = 20
    tmp = 1
    for i in range(n, n-x, -1):
        tmp = (tmp * i) % mod
    return tmp

def power_func(a,n,p=10**9+7):
    bi = str(format(n,"b"))#2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res*res) %p
        if bi[i] == "1":
            res = (res*a) %p
    return res


n,v=map(int,input().split())
m=10**9+7
perm1=deque([1])
perm2=deque([permutation(v,n)])
comb=deque([1])
ones=deque([1])
acc=((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
for k in range(n):
  #nCk+1=nCk*(n-k/k+1)
  comb.append((comb.pop()*(n-k)*power_func(k+1,m-2))%m)
  #vPk+1=vPk*(v-k)
  perm1.append((perm1.pop()*(v-k))%m)
  #v-(k+1)Pn-(k+1)=v-kPn-k/(v-k)
  perm2.append((perm2.pop()*power_func(v-k,m-2))%m)
  #(-1)^k+1=(-1)^k*(-1)
  ones.append(ones.pop()*(-1))
  acc+=ones[-1]*((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
print(acc%m)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
m=10**9+7
#r!(0>r>M)
lis=[0]
for q in range(1,n+1):
  lis.append(lis[-1]*q%m)
from functools import lru_cache
@lru_cache(maxsize=1000)
def comb(n,k,p):
  """power_funcを用いて(nCk) mod p を求める"""
  from math import factorial
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=factorial(n) %p
  b=factorial(k) %p
  c=factorial(n-k) %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p

@lru_cache(maxsize=1000)
def power_func(a,b,p):
  """a^b mod p を求める"""
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p ))%p
acc=0
for j in range(n+1):
  s=comb(n,j,m)
  t=s*(factorial(m)//factorial(j))%m
  acc+=((-1)**j)*t*(factorial(m-j)//factorial(n-j))**2%m
print(acc%m)


Fixed submission:
from collections import deque
#from functools import lru_cache

def permutation(n, x, mod=10**9+7):
    # nPx
    # 順列
    # ex) permutaion(5, 2) = 20
    tmp = 1
    for i in range(n, n-x, -1):
        tmp = (tmp * i) % mod
    return tmp

def power_func(a,n,p=10**9+7):
    bi = str(format(n,"b"))#2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res*res) %p
        if bi[i] == "1":
            res = (res*a) %p
    return res


n,v=map(int,input().split())
m=10**9+7
perm1=deque([1])
perm2=deque([permutation(v,n)])
comb=deque([1])
ones=deque([1])
acc=((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
for k in range(n):
  #nCk+1=nCk*(n-k/k+1)
  comb.append((comb.pop()*(n-k)*power_func(k+1,m-2))%m)
  #vPk+1=vPk*(v-k)
  perm1.append((perm1.pop()*(v-k))%m)
  #v-(k+1)Pn-(k+1)=v-kPn-k/(v-k)
  perm2.append((perm2.pop()*power_func(v-k,m-2))%m)
  #(-1)^k+1=(-1)^k*(-1)
  ones.append(ones.pop()*(-1))
  acc+=ones[-1]*((comb[-1]*perm1[-1]%m)*perm2[-1]**2)%m
print(acc%m)