Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = {i + 1:[] for i in range(n)}
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
com = 1
check_list = [1]
count = {i + 1:0 for i in range(n)}
while check_list:
    check = check_list.pop(0)
    next_check = graph[check]
    for i in range(len(next_check)):
        count[next_check[i]] += i
        check_list.append(next_check[i])
        graph[next_check[i]].remove(check)
    set_1 = set(graph[1])
    if check == 1:
        com *= k
    elif check in set_1:
        com *= k - 1 - count[check]
        com %= mod
    else:
        com *= k - 2 - count[check]
        com %= mod
print(com)

Fixed submission:
import collections

n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
flag = [False for i in range(n + 1)]
flag[1] = True
queue = collections.deque([1])
ans = k
while queue:
    test = queue.popleft()
    temp = [flag[graph[test][i]] for i in range(len(graph[test]))].count(True)
    num = 0
    for i in range(len(graph[test])):
        if flag[graph[test][i]] == True:
            continue
        ans *= (k - temp - num - 1)
        ans %= mod
        num += 1
        queue.append(graph[test][i])
        flag[graph[test][i]] = True
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = {i + 1:[] for i in range(n)}
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
com = 1
check_list = [1]
count = {i + 1:0 for i in range(n)}
check = check_list.pop(0)
next_check = graph[check]
for i in range(len(next_check)):
    count[next_check[i]] += i
    check_list.append(next_check[i])
    graph[next_check[i]].remove(check)
com *= k
while check_list:
    check = check_list.pop(0)
    next_check = graph[check]
    for i in range(len(next_check)):
        count[next_check[i]] += i
        check_list.append(next_check[i])
        graph[next_check[i]].remove(check)
    if check in graph[1]:
        com *= k - 1 - count[check]
        com %= mod
    else:
        com *= k - 2 - count[check]
        com %= mod
print(com)

Fixed submission:
import collections

n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
flag = [False for i in range(n + 1)]
flag[1] = True
queue = collections.deque([1])
ans = k
while queue:
    test = queue.popleft()
    temp = [flag[graph[test][i]] for i in range(len(graph[test]))].count(True)
    num = 0
    for i in range(len(graph[test])):
        if flag[graph[test][i]] == True:
            continue
        ans *= (k - temp - num - 1)
        ans %= mod
        num += 1
        queue.append(graph[test][i])
        flag[graph[test][i]] = True
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = {i + 1:set() for i in range(n)}
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a] |= set([b])
    graph[b] |= set([a])
com = 1
check_list = [1]
count = {i + 1:0 for i in range(n)}
while check_list:
    check = check_list.pop(0)
    next_check = list(graph[check])
    for i in range(len(next_check)):
        count[next_check[i]] += i
        check_list.append(next_check[i])
        graph[next_check[i]] -= set([check])
    if check == 1:
        com *= k
    elif check in graph[1]:
        com *= k - 1 - count[check]
        com %= mod
    else:
        com *= k - 2 - count[check]
        com %= mod
print(com)

Fixed submission:
import collections

n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
flag = [False for i in range(n + 1)]
flag[1] = True
queue = collections.deque([1])
ans = k
while queue:
    test = queue.popleft()
    temp = [flag[graph[test][i]] for i in range(len(graph[test]))].count(True)
    num = 0
    for i in range(len(graph[test])):
        if flag[graph[test][i]] == True:
            continue
        ans *= (k - temp - num - 1)
        ans %= mod
        num += 1
        queue.append(graph[test][i])
        flag[graph[test][i]] = True
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = {i + 1:[] for i in range(n)}
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
com = 1
check_list = [1]
count = {i + 1:0 for i in range(n)}
while check_list:
    check = check_list.pop(0)
    next_check = graph[check]
    for i in range(len(next_check)):
        count[next_check[i]] += i
        check_list.append(next_check[i])
        graph[next_check[i]].remove(check)
    if check == 1:
        com *= k
    elif check in graph[1]:
        com *= k - 1 - count[check]
        com %= mod
    else:
        com *= k - 2 - count[check]
        com %= mod
print(com)

Fixed submission:
import collections

n, k = map(int, input().split())
mod = 10 ** 9 + 7
graph = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
flag = [False for i in range(n + 1)]
flag[1] = True
queue = collections.deque([1])
ans = k
while queue:
    test = queue.popleft()
    temp = [flag[graph[test][i]] for i in range(len(graph[test]))].count(True)
    num = 0
    for i in range(len(graph[test])):
        if flag[graph[test][i]] == True:
            continue
        ans *= (k - temp - num - 1)
        ans %= mod
        num += 1
        queue.append(graph[test][i])
        flag[graph[test][i]] = True
print(ans)