Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())

MOD = 1000000007

g = [[] * 1 for i in range(n)]

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

used = [False for i in range(n)]

def dfs(v):
    used[v] = True
    ret = 1
    num = k-2
    for to in g[v]:
        if not used[to]:
            if num < 0:
                num = 0
            ret *= num * dfs(to) % MOD
            ret %= MOD
            num -= 1

    return ret

ans = k
num = k-1
used[0] = True
for to in g[0]:
    if not used[to]:
        if num < 0:
            num = 0
        ans *= num * dfs(to) % MOD
        ans %= MOD
        num -= 1

print(ans)


Fixed submission:
from collections import deque

n, k = map(int, input().split())

MOD = 1000000007

g = [[] * 1 for i in range(n)]

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

d = [-1 for i in range(n)]

q = deque([])
num = k-1
d[0] = k
for to in g[0]:
    if d[to] == -1:
        d[to] = num
        num -= 1
        if num < 0:
            num = 0
        q.append(to)

while q:
    v = q.popleft()
    num = k-2
    for to in g[v]:
        if d[to] == -1:
            d[to] = num
            num -= 1
            if num < 0:
                num = 0
            q.append(to)

ans = 1
for i in d:
    ans *= i
    ans %= MOD

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = input().split()
n = int(n)
k = int(k)

MOD = 1000000007

g = [[] for i in range(n)]

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

used = [False for i in range(n)]

def dfs(v):
    used[v] = True
    ret = 1
    num = k-2
    for to in g[v]:
        if not used[to]:
            if num < 0:
                num = 0
            ret *= num * dfs(to)
            num -= 1

    return ret

ans = k
num = k-1
used[0] = True
for to in g[0]:
    if not used[to]:
        if num < 0:
            num = 0
        ans *= num * dfs(to)
        num -= 1


print(ans%MOD)


Fixed submission:
from collections import deque

n, k = map(int, input().split())

MOD = 1000000007

g = [[] * 1 for i in range(n)]

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

d = [-1 for i in range(n)]

q = deque([])
num = k-1
d[0] = k
for to in g[0]:
    if d[to] == -1:
        d[to] = num
        num -= 1
        if num < 0:
            num = 0
        q.append(to)

while q:
    v = q.popleft()
    num = k-2
    for to in g[v]:
        if d[to] == -1:
            d[to] = num
            num -= 1
            if num < 0:
                num = 0
            q.append(to)

ans = 1
for i in d:
    ans *= i
    ans %= MOD

print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = input().split()
n = int(n)
k = int(k)

MOD = 1000000007

g = []
for i in range(n):
    g.append([])

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


used = [False for i in range(n)]


def dfs(v):
    used[v] = True
    ret = 1
    num = k-2
    for to in g[v]:
        if not used[to]:
            ret *= num * dfs(to) % MOD
            ret %= MOD
            num -= 1
            if num < 0:
                num = 0
    return ret

ans = k
num = k-1
used[0] = True
for to in g[0]:
    if not used[to]:
        ans *= num * dfs(to) % MOD
        ans %= MOD
        num -= 1
        if num < 0:
            num = 0

print(ans)


Fixed submission:
from collections import deque

n, k = map(int, input().split())

MOD = 1000000007

g = [[] * 1 for i in range(n)]

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

d = [-1 for i in range(n)]

q = deque([])
num = k-1
d[0] = k
for to in g[0]:
    if d[to] == -1:
        d[to] = num
        num -= 1
        if num < 0:
            num = 0
        q.append(to)

while q:
    v = q.popleft()
    num = k-2
    for to in g[v]:
        if d[to] == -1:
            d[to] = num
            num -= 1
            if num < 0:
                num = 0
            q.append(to)

ans = 1
for i in d:
    ans *= i
    ans %= MOD

print(ans)