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

AB = []
for _ in range(1, N):
    a, b = map(int, input().split())
    AB.append((a, b))
    
# 無効グラフなので双方向にする
graph = {}
for a,b in AB:
    if not a in graph:
        graph[a] = {}
    if not b in graph:
        graph[b] = {}
    graph[a][b] = 1
    graph[b][a] = 1


#print(graph)

collors = [None] * (N+1)
from collections import deque
q = deque([(K, 1)]) 
while q:
    col_num, v = q.popleft()
    if collors[v] == None:
        collors[v] = col_num
        
        if col_num == K:
            rest_col = K-1
        else:
            rest_col = K-2
        
        for v in graph[v]:
            if collors[v] == None:
                q.appendleft((rest_col, v))
                rest_col -= 1
#print(collors)  

MOD = 10**9+7

ans = 1
for i in collors[1:]:
    ans = ans * i % MOD
print(ans)
    

Fixed submission:
N, K = map(int, input().split())

AB = []
for _ in range(1, N):
    a, b = map(int, input().split())
    AB.append((a, b))
    
if N == 1:
    print(K)
else:
    # 無効グラフなので双方向にする
    graph = {}
    for a,b in AB:
        if not a in graph:
            graph[a] = {}
        if not b in graph:
            graph[b] = {}
        graph[a][b] = 1
        graph[b][a] = 1


    #print(graph)

    collors = [None] * (N+1)
    from collections import deque
    q = deque([(K, 1)]) 
    while q:
        col_num, v = q.popleft()
        if collors[v] == None:
            collors[v] = col_num

            if col_num == K:
                rest_col = K-1
            else:
                rest_col = K-2

            for v in graph[v]:
                if collors[v] == None:
                    q.appendleft((rest_col, v))
                    rest_col -= 1
    #print(collors)  

    MOD = 10**9+7

    ans = 1
    for i in collors[1:]:
        ans = ans * i % MOD
    print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())


AB = []
for _ in range(1, N):
    a, b = map(int, input().split())
    AB.append((a, b))
    
# 無効グラフなので双方向にする
graph = {}
for a,b in AB:
    if not a in graph:
        graph[a] = {}
    if not b in graph:
        graph[b] = {}
    graph[a][b] = 1
    graph[b][a] = 1


from heapq import heappush, heappop

def dijkstra(graph, start, N):
    # path_lenが色数
    A = [None] * (N+1)
    queue = [(K, start)]
    while queue:
        path_len, v = heappop(queue)
        if A[v] is None: # v is unvisited
            A[v] = path_len
            if path_len == K:
                path_len = K
            else:
                path_len = K-1
            for w, edge_len in graph[v].items():
                if A[w] is None:
                    path_len -= 1
                    heappush(queue, (path_len, w))

    # to give same result as original, assign zero distance to unreachable vertices             
    return [0 if x is None else x for x in A]

shortest = dijkstra(graph, 1, N)

ans = 1
for i in shortest[1:]:
    ans *= i
print(ans % 1000000007)
    

Fixed submission:
N, K = map(int, input().split())

AB = []
for _ in range(1, N):
    a, b = map(int, input().split())
    AB.append((a, b))
    
if N == 1:
    print(K)
else:
    # 無効グラフなので双方向にする
    graph = {}
    for a,b in AB:
        if not a in graph:
            graph[a] = {}
        if not b in graph:
            graph[b] = {}
        graph[a][b] = 1
        graph[b][a] = 1


    #print(graph)

    collors = [None] * (N+1)
    from collections import deque
    q = deque([(K, 1)]) 
    while q:
        col_num, v = q.popleft()
        if collors[v] == None:
            collors[v] = col_num

            if col_num == K:
                rest_col = K-1
            else:
                rest_col = K-2

            for v in graph[v]:
                if collors[v] == None:
                    q.appendleft((rest_col, v))
                    rest_col -= 1
    #print(collors)  

    MOD = 10**9+7

    ans = 1
    for i in collors[1:]:
        ans = ans * i % MOD
    print(ans)