Buggy python submission (verdict: Runtime Error):
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

def dfs(K, G, now, parent):
    can_use_color_num = K-2
    if parent == -1:
        can_use_color_num = K-1
    if K < len(G[now]):
        return 0
    else:
        case_num = 1
        for e in G[now]:
            if e == parent:
                continue
            case_num *= can_use_color_num
            can_use_color_num -= 1
            case_num %= MOD
        for e in G[now]:
            if e == parent:
                continue
            case_num *= dfs(K, G, e, now)
            case_num %= MOD
        return case_num

ans = K * dfs(K, G, 0, -1)
print(ans % MOD)

Fixed submission:
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

C = [False for _ in range(N)]
F = [0]
C[0] = True
r = K
while F:
    n = F.pop()
    if n == 0:
        c = K-1
    else:
        c = K-2
    for e in G[n]:
        if C[e]:
            continue
        C[e] = True
        r = (r * c) % MOD
        c -= 1
        F.append(e)

print(r)

Buggy python submission (verdict: Runtime Error):
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

def dfs(K, G, now, parent):
    can_use_color_num = K-2
    if parent == -1:
        can_use_color_num = K-1
    if K < len(G[now]):
        return 0
    else:
        case_num = 1
        for e in G[now]:
            if e == parent:
                continue
            case_num *= can_use_color_num
            can_use_color_num -= 1
            case_num %= MOD
        for e in G[now]:
            if e == parent:
                continue
            case_num *= dfs(K, G, e, now)
            case_num %= MOD
        return case_num

ans = K * dfs(K, G, 0, -1)
print(ans % MOD)

Fixed submission:
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

C = [False for _ in range(N)]
F = [0]
C[0] = True
r = K
while F:
    n = F.pop()
    if n == 0:
        c = K-1
    else:
        c = K-2
    for e in G[n]:
        if C[e]:
            continue
        C[e] = True
        r = (r * c) % MOD
        c -= 1
        F.append(e)

print(r)

Buggy python submission (verdict: Runtime Error):
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

def dfs(K, G, now, parent):
    can_use_color_num = K-2
    if parent == -1:
        can_use_color_num = K-1
    if K < len(G[now]):
        return 0
    else:
        case_num = 1
        for e in G[now]:
            if e == parent:
                continue
            case_num *= can_use_color_num
            can_use_color_num -= 1
            case_num %= MOD
        for e in G[now]:
            if e == parent:
                continue
            case_num *= dfs(K, G, e, now)
            case_num %= MOD
        print("now:{},num:{}".format(now, case_num))
        return case_num

ans = K * dfs(K, G, 0, -1)
print(ans % MOD)

Fixed submission:
MOD = 1000000007
N, K = map(int,input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)

C = [False for _ in range(N)]
F = [0]
C[0] = True
r = K
while F:
    n = F.pop()
    if n == 0:
        c = K-1
    else:
        c = K-2
    for e in G[n]:
        if C[e]:
            continue
        C[e] = True
        r = (r * c) % MOD
        c -= 1
        F.append(e)

print(r)