Problem p02985 (python)

Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, k = map(int, readline().split())


graph = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
    cnt[a] += 1
    cnt[b] += 1
ans = 1

mod = 10 ** 9 + 7
kaidan = [1] * (10 ** 5 + 1)
gyaku = [1] * (10 ** 5 + 1)

for i in range(1, 10 ** 5 + 1):
    kaidan[i] = i * kaidan[i - 1] % mod
    gyaku[i] = pow(kaidan[i], mod - 2, mod)


def perm(k, i):
    if i > k:
        return 0
    return kaidan[k] * gyaku[k - i]


# dfs

stack = [1]
used = [False] * (n + 1)
while stack:
    v = stack.pop()
    used[v] = True
    if v == 1:
        ans *= k * perm(k - 1, cnt[v])
        ans %= mod
    else:
        ans *= perm(k - 2, cnt[v] - 1)
        ans %= mod
    for u in graph[v]:
        if used[u] == True:
            continue
        stack.append(u)

print(ans)

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, k = map(int, readline().split())


graph = [[] for _ in range(n + 1)]
cnt = [0] * (n + 1)
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
    cnt[a] += 1
    cnt[b] += 1
ans = 1

mod = 10 ** 9 + 7
kaidan = [1] * (n + 1)
gyaku = [1] * (n + 1)

for i in range(1, n + 1):
    kaidan[i] = i * kaidan[i - 1] % mod
    gyaku[i] = pow(kaidan[i], mod - 2, mod)


def perm(k, i):
    if i > k:
        return 0
    return kaidan[k] * gyaku[k - i]


# dfs

stack = [1]
used = [False] * (n + 1)
while stack:
    v = stack.pop()
    used[v] = True
    if v == 1:
        ans *= k * perm(k - 1, cnt[v])
        ans %= mod
    else:
        ans *= perm(k - 2, cnt[v] - 1)
        ans %= mod
    for u in graph[v]:
        if used[u] == True:
            continue
        stack.append(u)

print(ans)

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 + 7
n, k, *ab = map(int, read().split())
m = iter(ab)
AB = zip(m, m)
graph = [[] for _ in range(n + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)
ans = 1

kaidan = [0] * (k + 1)
kaidan[0] = 1
for i in range(1, k + 1):
    kaidan[i] = i * kaidan[i - 1]


def perm(k, i):
    if i > k:
        return 0
    return kaidan[k] // kaidan[k - i]


def dfs(v, p):
    global ans
    if p == -1:
        ans *= k * perm(k - 1, len(graph[v]))
        ans %= mod
    else:
        ans *= perm(k - 2, len(graph[v]) - 1)
        ans %= mod
    for u in graph[v]:
        if u == p:
            continue
        dfs(u, v)


dfs(1, -1)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 + 7
n, k, *ab = map(int, read().split())
m = iter(ab)
AB = zip(m, m)
graph = [[] for _ in range(n + 1)]
for a, b in AB:
    graph[a].append(b)
    graph[b].append(a)
ans = 1

kaidan = [0] * (k + 1)
kaidan[0] = 1
for i in range(1, k + 1):
    kaidan[i] = i * kaidan[i - 1]


def perm(k, i):
    if i > k:
        return 0
    return kaidan[k] // kaidan[k - i]


def dfs(v, p):
    global ans
    if p == -1:
        ans *= k * perm(k - 1, len(graph[v]))
        ans %= mod
    else:
        ans *= perm(k - 2, len(graph[v]) - 1)
        ans %= mod
    for u in graph[v]:
        if u == p:
            continue
        dfs(u, v)


dfs(1, -1)
print(ans)