Buggy python submission (verdict: Runtime Error):
import sys
from collections import Counter
input = sys.stdin.readline
N, K = map(int, input().split())
mod = 10 ** 9 + 7
e = [[] for _ in range(N + 1)]
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

s = [1]
vis = [0] * (N + 1)
order = []
parent = [0] * (N + 1)
children = [0] * (N + 1)
while len(s):
  x = s.pop()
  vis[x] = 1
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    s.append(y)
    parent[y] = x
    children[x] += 1
cs = Counter()
cr = children[1]
for y in range(2, N + 1): cs[children[y]] += 1
#print(cs, cr)
res = [0] * (K + 1)
l = 0
csmx = max(cs.keys())

for c in range(K + 1):
  if c <= 1 and (cr > 0): continue
  if c <= 2 and (len(cs) > 0): continue
  if c - 1 >= cr and (c - 2 >= csmx):
    l = c
    break
if l == 0:
  print(0)
  exit(0)

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(K, mod)
smcs = sum(cs.values())
res[l] = f.factorial(l) * f.ifactorial(l - cr - 1) % mod
for k, v in cs.items():
  res[l] *= pow(f.factorial(l - 2) * f.ifactorial(l - k - 2) % mod, v, mod)
  res[l] %= mod
#print(res)
revs = [0] * (K + 1)
for i in range(K + 1): revs[i] = pow(i, mod - 2, mod)
for i in range(l, K):
  res[i + 1] = res[i] * pow(i - 1, smcs, mod) % mod
  res[i + 1] *= (i + 1) * revs[i - cr] % mod
  res[i + 1] %= mod
  for k, v in cs.items():
    res[i + 1] *= pow(revs[i - k - 1], v, mod)
    res[i + 1] %= mod

print(res[-1])

Fixed submission:
from collections import defaultdict as ddydd
from collections import deque as dq
N, K = map(int, input().split())
mod = 10 ** 9 + 7
Tree = ddydd(list)
for _ in range(N - 1):
  a, b = map(int, input().split())
  Tree[a].append(b)
  Tree[b].append(a)
Q = dq(list())
visited = [0] * (N + 1)
visited[1] = 1
res = K
temp = K - 1
for n in Tree[1]:
  visited[n] = 1
  Q.append(n)
  res *= temp
  res = res % mod
  temp -= 1

while Q:
  n = Q.popleft()
  temp = K - 2
  for m in Tree[n]:
    if visited[m] == 0:
      visited[m] = 1
      Q.append(m)
      res *= temp
      res = res % mod
      temp -= 1
print(res)

Buggy python submission (verdict: Runtime Error):
import sys
from collections import Counter
input = sys.stdin.readline
N, K = map(int, input().split())
mod = 10 ** 9 + 7
e = [[] for _ in range(N + 1)]
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  e[v].append(u)

s = [1]
vis = [0] * (N + 1)
order = []
parent = [0] * (N + 1)
children = [0] * (N + 1)
while len(s):
  x = s.pop()
  vis[x] = 1
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    s.append(y)
    parent[y] = x
    children[x] += 1
cs = Counter()
cr = children[1]
for y in range(2, N + 1): cs[children[y]] += 1
#print(cs, cr)
res = [0] * (K + 1)
l = 0
csmx = max(cs.keys())

for c in range(K + 1):
  if c <= 1 and (cr > 0): continue
  if c <= 2 and (len(cs) > 0): continue
  if c - 1 >= cr and (c - 2 >= csmx):
    l = c
    break
#print(l)

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(K, mod)
smcs = sum(cs.values())
res[l] = f.factorial(l) * f.ifactorial(l - cr - 1) % mod
for k, v in cs.items():
  res[l] *= pow(f.factorial(l - 2) * f.ifactorial(l - k - 2) % mod, v, mod)
  res[l] %= mod
#print(res)
revs = [0] * (K + 1)
for i in range(K + 1): revs[i] = pow(i, mod - 2, mod)
for i in range(l, K):
  res[i + 1] = res[i] * pow(i - 1, smcs, mod) % mod
  res[i + 1] *= (i + 1) * revs[i - cr] % mod
  res[i + 1] %= mod
  for k, v in cs.items():
    res[i + 1] *= pow(revs[i - k - 1], v, mod)
    res[i + 1] %= mod

print(res[-1])

Fixed submission:
from collections import defaultdict as ddydd
from collections import deque as dq
N, K = map(int, input().split())
mod = 10 ** 9 + 7
Tree = ddydd(list)
for _ in range(N - 1):
  a, b = map(int, input().split())
  Tree[a].append(b)
  Tree[b].append(a)
Q = dq(list())
visited = [0] * (N + 1)
visited[1] = 1
res = K
temp = K - 1
for n in Tree[1]:
  visited[n] = 1
  Q.append(n)
  res *= temp
  res = res % mod
  temp -= 1

while Q:
  n = Q.popleft()
  temp = K - 2
  for m in Tree[n]:
    if visited[m] == 0:
      visited[m] = 1
      Q.append(m)
      res *= temp
      res = res % mod
      temp -= 1
print(res)