Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 6))
from collections import deque

MOD = pow(10, 9) + 7
ans = 1
n, k = map(int, input().split())
visited = [False for _ in range(n)]
edges = {}

def dfs(i, c1, c2):
  global ans
  if visited[i]:
    return
  visited[i] = True
  ans = ans * (k-c1-c2) % MOD
  c0 = 0
  for e in edges[i]:
    if not visited[e]:
      dfs(e, 1, c1+c0)
      c0 += 1


def main():
  for _ in range(n-1):
    a, b = map(int, input().split())
    if a-1 not in edges:
      edges[a-1] = [b-1]
    else:
      edges[a-1].append(b-1)
    if b-1 not in edges:
      edges[b-1] = [a-1]
    else:
      edges[b-1].append(a-1)
  # start
  for i in range(n):
    if len(edges[i]) == 1:
      dfs(i, 0, 0)
  print(ans)


if __name__ == '__main__':
  main()


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 7))

MOD = pow(10, 9) + 7
ans = 1
n, k = map(int, input().split())
visited = [False for _ in range(n)]
edges = {}

def dfs(i, c1, c2):
  global ans
  if visited[i]:
    return
  visited[i] = True
  ans = ans * (k-c1-c2) % MOD
  c0 = 0
  for e in edges[i]:
    if not visited[e]:
      dfs(e, 1, c1+c0)
      c0 += 1


def main():
  for _ in range(n-1):
    a, b = map(int, input().split())
    if a-1 not in edges:
      edges[a-1] = [b-1]
    else:
      edges[a-1].append(b-1)
    if b-1 not in edges:
      edges[b-1] = [a-1]
    else:
      edges[b-1].append(a-1)
  # start
  if n == 1:
    print(k)
  else:
    for i in range(n):
      if len(edges[i]) == 1:
        dfs(i, 0, 0)
    print(ans)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
from collections import deque

MOD = pow(10, 9) + 7
ans = 1
n, k = map(int, input().split())
visited = [False for _ in range(n)]
edges = {}

def dfs(i, c1, c2):
  global ans
  if visited[i]:
    return
  visited[i] = True
  ans = ans * (k-c1-c2) % MOD
  c0 = 0
  for e in edges[i]:
    if not visited[e]:
      dfs(e, 1, c1+c0)
      c0 += 1


def main():
  for _ in range(n-1):
    a, b = map(int, input().split())
    if a-1 not in edges:
      edges[a-1] = [b-1]
    else:
      edges[a-1].append(b-1)
    if b-1 not in edges:
      edges[b-1] = [a-1]
    else:
      edges[b-1].append(a-1)
  # start
  for i in range(n):
    if len(edges[i]) == 1:
      dfs(i, 0, 0)
  print(ans)


if __name__ == '__main__':
  main()


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(pow(10, 7))

MOD = pow(10, 9) + 7
ans = 1
n, k = map(int, input().split())
visited = [False for _ in range(n)]
edges = {}

def dfs(i, c1, c2):
  global ans
  if visited[i]:
    return
  visited[i] = True
  ans = ans * (k-c1-c2) % MOD
  c0 = 0
  for e in edges[i]:
    if not visited[e]:
      dfs(e, 1, c1+c0)
      c0 += 1


def main():
  for _ in range(n-1):
    a, b = map(int, input().split())
    if a-1 not in edges:
      edges[a-1] = [b-1]
    else:
      edges[a-1].append(b-1)
    if b-1 not in edges:
      edges[b-1] = [a-1]
    else:
      edges[b-1].append(a-1)
  # start
  if n == 1:
    print(k)
  else:
    for i in range(n):
      if len(edges[i]) == 1:
        dfs(i, 0, 0)
    print(ans)


if __name__ == '__main__':
  main()