Buggy python submission (verdict: Time Limit Exceeded):
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


# print(ans)
prev = 1
ansB = M-1

for i in range(1, N):
    tmp = (prev)*i

    tmp2 = ansB - tmp
    # print(tmp2)

    ansB = tmp * (M-i) + (tmp2)*(M-i-1)
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)
# print(ansB)

print((ans*ansB) % mod)


Fixed submission:
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


# print(ans)
prev = 1
ansB = M-1


for i in range(1, N):
    tmp = (prev)*i % mod
    tmp2 = ansB - tmp % mod
    # print(tmp2)
    ansB = tmp * (M-i) % mod
    ansB += (tmp2)*(M-i-1) % mod
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)

# print(ansB)

print((ans*ansB) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
mod = 10**9 + 7
# 一個数列が決まった場合に、その数列と同じ数字が同じ場所にあるものが禁止される。


MPN = [0]*M
MPN[0] = M
for i in range(M-1, 0, -1):
    MPN[M - i] = MPN[M-1-i]*i % mod

ans = MPN[N-1]
# print(ans)
prev = 1
ansB = M-1

for i in range(1, N):
    tmp = (prev)*i

    tmp2 = ansB - tmp
    # print(tmp2)

    ansB = tmp * (M-i) + (tmp2)*(M-i-1)
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)
# print(ansB)

print((ans*ansB) % mod)


Fixed submission:
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


# print(ans)
prev = 1
ansB = M-1


for i in range(1, N):
    tmp = (prev)*i % mod
    tmp2 = ansB - tmp % mod
    # print(tmp2)
    ansB = tmp * (M-i) % mod
    ansB += (tmp2)*(M-i-1) % mod
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)

# print(ansB)

print((ans*ansB) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
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, N + 1):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)


MPN = [0]*M
MPN[0] = M
for i in range(M-1, 0, -1):
    MPN[M - i] = MPN[M-1-i]*i % mod

ans = MPN[N-1]
# print(ans)
prev = 1
ansB = M-1

for i in range(1, N):
    tmp = (prev)*i

    tmp2 = ansB - tmp
    # print(tmp2)

    ansB = tmp * (M-i) + (tmp2)*(M-i-1)
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)
# print(ansB)

print((ans*ansB) % mod)


Fixed submission:
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


# print(ans)
prev = 1
ansB = M-1


for i in range(1, N):
    tmp = (prev)*i % mod
    tmp2 = ansB - tmp % mod
    # print(tmp2)
    ansB = tmp * (M-i) % mod
    ansB += (tmp2)*(M-i-1) % mod
    # print(tmp, tmp2)
    # ansB = tmp + tmp2
    ansB %= mod
    prev = tmp2
    # print(ansB)

# print(ansB)

print((ans*ansB) % mod)