Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque([0])
already = [False]*n
yet = [False]*n

parent = [0]*n
already[0] = True

ans = 1
while not_yet:
  key = not_yet.popleft()
  yet[key] = True
  next_count = 1
  for value in tree[key]:
    if not already[value]:
      not_yet.append(value)
      already[value] = True
      parent[value] = key
  for value in tree[parent[key]]:
    if yet[value] and value != key:
      next_count += 1
  
  if key == 0:
    next_count = 0
  ans *= k-next_count
  ans %= inf

print(ans)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque(tree[0])
already = [False]*n
already[0] = True
ans = k
key = k-1
for v in tree[0]:
  already[v] = True
  ans *= key
  ans %= inf
  key -= 1

while not_yet:
  key = not_yet.popleft()
  for v in range(k-2, k-1-len(tree[key]), -1):
    ans *= v
    ans %= inf
  
  for value in tree[key]:
    if already[value]:
      continue
    not_yet.append(value)
    already[value] = True

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque([0])
already = [False]*n
yet = [False]*n

parent = [0]*n
already[0] = True

ans = 1
while not_yet:
  key = not_yet.popleft()
  yet[key] = True
  next_count = 1
  for value in tree[key]:
    if not already[value]:
      not_yet.append(value)
      already[value] = True
      parent[value] = key
  for value in tree[parent[key]]:
    if yet[value] and value != key:
      next_count += 1
  
  if key == 0:
    next_count = 0
  ans *= k-next_count
  ans %= inf

print(ans)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque(tree[0])
already = [False]*n
already[0] = True
ans = k
key = k-1
for v in tree[0]:
  already[v] = True
  ans *= key
  ans %= inf
  key -= 1

while not_yet:
  key = not_yet.popleft()
  for v in range(k-2, k-1-len(tree[key]), -1):
    ans *= v
    ans %= inf
  
  for value in tree[key]:
    if already[value]:
      continue
    not_yet.append(value)
    already[value] = True

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

tree_next = [0 for _ in range(n)]
for i in range(n):
  key = set()
  for value in tree[i]:
    key.add(value)
    for value2 in tree[value]:
      key.add(value2)
  key.discard(i)
  tree_next[i] = key
not_yet = deque([0])
already = [False]*n
already_list = set()
already[0] = True

ans = 1
while not_yet:
  key = not_yet.popleft()
  next_key = k-len(already_list&tree_next[key])
  ans *= next_key
  ans %= inf
  already_list.add(key)
  for value in tree[key]:
    if not already[value]:
      not_yet.append(value)
      already[value] = True

print(ans)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque(tree[0])
already = [False]*n
already[0] = True
ans = k
key = k-1
for v in tree[0]:
  already[v] = True
  ans *= key
  ans %= inf
  key -= 1

while not_yet:
  key = not_yet.popleft()
  for v in range(k-2, k-1-len(tree[key]), -1):
    ans *= v
    ans %= inf
  
  for value in tree[key]:
    if already[value]:
      continue
    not_yet.append(value)
    already[value] = True

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque([0])
already = [False]*n
next_count = [0]*n

already[0] = True

ans = 1
while not_yet:
  key = not_yet.popleft()
  ans *= k-next_count[key]
  ans %= inf
  
  for value in tree[key]:
    next_count[value] += 1
    if not already[value]:
      not_yet.append(value)
      already[value] = True
    for value2 in tree[value]:
      next_count[value2] += 1

print(ans)

Fixed submission:
from collections import deque
n, k = map(int, input().split())
inf = pow(10, 9)+7

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

not_yet = deque(tree[0])
already = [False]*n
already[0] = True
ans = k
key = k-1
for v in tree[0]:
  already[v] = True
  ans *= key
  ans %= inf
  key -= 1

while not_yet:
  key = not_yet.popleft()
  for v in range(k-2, k-1-len(tree[key]), -1):
    ans *= v
    ans %= inf
  
  for value in tree[key]:
    if already[value]:
      continue
    not_yet.append(value)
    already[value] = True

print(ans)