Buggy python submission (verdict: Runtime Error):
MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


ans = K
q = [(1, None, 1)]
while len(q) > 0:
    par, parpar, depth = q.pop()
    children = edge[par] - set([parpar])
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        ans *= start - i
        ans %= MOD
    for c in children:
        q.append((c, par, depth + 1))

print(ans)


Fixed submission:
MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


ans = K
q = [(1, None, 1)]
while len(q) > 0:
    par, parpar, depth = q.pop()
    if par not in edge:
        continue
    children = edge[par] - set([parpar])
    if len(children) == 0:
        continue
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        if start - i <= 0:
            ans = 0
            print(0)
            exit()
        else:
            ans *= start - i
        ans %= MOD
    for c in children:
        q.append((c, par, depth + 1))

print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(4100000)

MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


def dig(par, parpar=None, depth=1):
    global ans
    children = edge[par] - set([parpar])
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        ans *= start - i
        ans %= MOD
    for c in children:
        dig(c, par, depth + 1)

ans = K
dig(1)
print(ans)


Fixed submission:
MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


ans = K
q = [(1, None, 1)]
while len(q) > 0:
    par, parpar, depth = q.pop()
    if par not in edge:
        continue
    children = edge[par] - set([parpar])
    if len(children) == 0:
        continue
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        if start - i <= 0:
            ans = 0
            print(0)
            exit()
        else:
            ans *= start - i
        ans %= MOD
    for c in children:
        q.append((c, par, depth + 1))

print(ans)

Buggy python submission (verdict: Runtime Error):
MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


def dig(par, parpar=None, depth=1):
    global ans
    children = edge[par] - set([parpar])
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        ans *= start - i
        ans %= MOD
    for c in children:
        dig(c, par, depth + 1)

ans = K
dig(1)
print(ans)


Fixed submission:
MOD = 1000000007
N, K = map(int, input().split())
edge = {}
for _ in range(N - 1):
    a, b = map(int, input().split())
    if a not in edge:
        edge[a] = set()
    edge[a].add(b)
    if b not in edge:
        edge[b] = set()
    edge[b].add(a)


ans = K
q = [(1, None, 1)]
while len(q) > 0:
    par, parpar, depth = q.pop()
    if par not in edge:
        continue
    children = edge[par] - set([parpar])
    if len(children) == 0:
        continue
    if depth == 1:
        start = K - 1
    else:
        start = K - 2
    for i in range(len(children)):
        if start - i <= 0:
            ans = 0
            print(0)
            exit()
        else:
            ans *= start - i
        ans %= MOD
    for c in children:
        q.append((c, par, depth + 1))

print(ans)