Buggy python submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache(maxsize=10**11)
def fact(x):
    return math.factorial(x)

@lru_cache(maxsize=10**11)
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)


Fixed submission:
N, K = map(int, input().split())
graph = [[] for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

stack = [0]
ans = K
mod = 10**9+7
r = [True] * N
r[0] = False
while stack:
    v = stack.pop()
    tmp = K - 1
    if v > 0:
        tmp -= 1
    for child in graph[v]:
        if r[child]:
            r[child] = False
            stack.append(child)
            ans *= tmp
            tmp -= 1
            ans %= mod
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache(maxsize=10**6)
def fact(x):
    return math.factorial(x)

@lru_cache(maxsize=10**6)
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)


Fixed submission:
N, K = map(int, input().split())
graph = [[] for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

stack = [0]
ans = K
mod = 10**9+7
r = [True] * N
r[0] = False
while stack:
    v = stack.pop()
    tmp = K - 1
    if v > 0:
        tmp -= 1
    for child in graph[v]:
        if r[child]:
            r[child] = False
            stack.append(child)
            ans *= tmp
            tmp -= 1
            ans %= mod
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
from functools import lru_cache
mod = 10 ** 9 + 7
N, K = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

@lru_cache()
def fact(x):
    return math.factorial(x)

@lru_cache()
def permutation_count(n, r):
    return fact(n) // fact(n-r)

stack = []
for v in graph[0]:
    stack.append((v, 0))
ans = K * permutation_count(K-1, len(graph[0]))
while stack:
    v, parent = stack.pop()
    ans *= permutation_count(K-2, len(graph[v])-1)
    ans %= mod
    for child in graph[v]:
        if child == parent:
            continue
        stack.append((child, v))

print(ans)


Fixed submission:
N, K = map(int, input().split())
graph = [[] for i in range(N)]

for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)

stack = [0]
ans = K
mod = 10**9+7
r = [True] * N
r[0] = False
while stack:
    v = stack.pop()
    tmp = K - 1
    if v > 0:
        tmp -= 1
    for child in graph[v]:
        if r[child]:
            r[child] = False
            stack.append(child)
            ans *= tmp
            tmp -= 1
            ans %= mod
print(ans)