Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
  input = sys.stdin.readline
  sys.setrecursionlimit(10**7)
  n,k = map(int,input().split())
  sl = [0]*n
  tree = [[] for i in range(n)]
  mod = 10**9+7

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

  def perm(n, k = 1):
    pdt = 1
    for x in range(n, n-k, -1):
      pdt *= x
    return pdt

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      ans *= perm(k-1,child)
    else:    
      ans *= perm(k-2,child)
    ans %= mod
    return ans

  print(dfs(0,0,k)*k%mod)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
  input = sys.stdin.readline
  sys.setrecursionlimit(10**7)
  n,k = map(int,input().split())
  sl = [0]*n
  dl = [0]*n
  tree = [[] for i in range(n)]
  mod = 10**9+7

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

  def perm(n, k = 1):
    pdt = 1
    if n != int(n) or k != int(k):
      raise ValueError("perm() not defined for float object")
    elif k < 0:
      pdt = 0
    elif n < 0:
      pdt = 0
    else:
      for x in range(n, n-k, -1):
        pdt *= x
    return pdt

  def dfs(x,depth,k):
    sl[x] = 1
    dl[depth] += 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        ans %= mod
        child += 1
    if depth == 0:
      ans *= perm(k-1,child)
      ans %= mod
    else:    
      ans *= perm(k-2,child)
      ans %= mod
    return ans

  print(dfs(0,0,k)*k%mod)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
  input = sys.stdin.readline
  sys.setrecursionlimit(10**7)
  n,k = map(int,input().split())
  sl = [0]*n
  dl = [0]*n
  tree = [[] for i in range(n)]
  mod = 10**9+7

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

  def perm(n, k = 1):
    pdt = 1
    if n != int(n) or k != int(k):
      raise ValueError("perm() not defined for float object")
    elif k < 0:
      pdt = 0
    elif n < 0:
      pdt = 0
    else:
      for x in range(n, n-k, -1):
        pdt *= x
    return pdt

  def dfs(x,depth,k):
    sl[x] = 1
    dl[depth] += 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        ans %= mod
        child += 1
    if depth == 0:
      ans *= perm(k-1,child)
      ans %= mod
    else:    
      ans *= perm(k-2,child)
      ans %= mod
    return ans

  print(dfs(0,0,k)*k%mod)

if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n,k = map(int,input().split())
sl = [0]*n
dl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7

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

def perm(n, k = 1):
  pdt = 1
  
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  
  elif k < 0:
    pdt = 0
  
  elif n < 0:
    pdt = 0
  
  else:
    for x in range(n, n-k, -1):
      pdt *= x
      
  return pdt

def dfs(x,depth,k):
  sl[x] = 1
  dl[depth] += 1
  child = 0
  ans = 1
  for i in tree[x]:
    if sl[i] == 0:
      ans *= dfs(i,depth+1,k)
      ans %= mod
      child += 1
  if depth == 0:
    ans *= perm(k-1,child)
    ans %= mod
  else:    
    ans *= perm(k-2,child)
    ans %= mod
  return ans

print(dfs(0,0,k)*k%mod)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n,k = map(int,input().split())
sl = [0]*n
dl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7

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

def perm(n, k = 1):
  pdt = 1
  
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  
  elif k < 0:
    pdt = 0
  
  elif n < 0:
    pdt = 0
  
  else:
    for x in range(n, n-k, -1):
      pdt *= x
      
  return pdt

def dfs(x,depth,k):
  sl[x] = 1
  dl[depth] += 1
  child = 0
  ans = 1
  for i in tree[x]:
    if sl[i] == 0:
      ans *= dfs(i,depth+1,k)
      ans %= mod
      child += 1
  if depth == 0:
    ans *= perm(k-1,child)
    ans %= mod
  else:    
    ans *= perm(k-2,child)
    ans %= mod
  return ans

print(dfs(0,0,k)*k%mod)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n,k = map(int,input().split())
sl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7
q = []
ans = k

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

def perm(n, k = 1):
  pdt = 1
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  elif k < 0:
    pdt = 0
  elif n < 0:
    pdt = 0
  else:
    for x in range(n, n-k, -1):
      pdt *= x
  return pdt

if len(tree[0]) > 0:
  q = tree[0]
  ans = k*perm(k-1,len(tree[0]))%mod
  sl[0] = 1
else:
  print(k)
  exit()
  
while q:
  pos = q.pop(0)
  sl[pos] = 1
  lent = len(tree[pos]) - 1
  if lent > 0:
    ans = ans*perm(k-2,lent)%mod
  for i in tree[pos]:
    if sl[i] == 0:
      q.append(i)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
sl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7
q = []
ans = k

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

def perm(n, k = 1):
  pdt = 1
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  elif k < 0:
    pdt = 0
  elif n < 0:
    pdt = 0
  else:
    for x in range(n, n-k, -1):
      pdt *= x
  return pdt

if len(tree[0]) > 0:
  q = tree[0]
  ans = k*perm(k-1,len(tree[0]))%mod
  sl[0] = 1
else:
  print(k)
  exit()
  
while q:
  pos = q.pop(0)
  sl[pos] = 1
  lent = len(tree[pos]) - 1
  if lent > 0:
    ans = ans*perm(k-2,lent)%mod
  for i in tree[pos]:
    if sl[i] == 0:
      q.append(i)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
n,k = map(int,input().split())
sl = [0]*n
dl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7

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

def perm(n, k = 1):
  pdt = 1
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  elif k < 0:
    pdt = 0
  elif n < 0:
    pdt = 0
  else:
    for x in range(n, n-k, -1):
      pdt *= x
  return pdt

def dfs(x,depth,k):
  sl[x] = 1
  dl[depth] += 1
  child = 0
  ans = 1
  for i in tree[x]:
    if sl[i] == 0:
      ans *= dfs(i,depth+1,k)
      ans %= mod
      child += 1
  if depth == 0:
    ans *= perm(k-1,child)
    ans %= mod
  else:    
    ans *= perm(k-2,child)
    ans %= mod
  return ans

print(dfs(0,0,k)*k%mod)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
n,k = map(int,input().split())
sl = [0]*n
dl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7

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

def perm(n, k = 1):
  pdt = 1
  
  if n != int(n) or k != int(k):
    raise ValueError("perm() not defined for float object")
  
  elif k < 0:
    pdt = 0
  
  elif n < 0:
    pdt = 0
  
  else:
    for x in range(n, n-k, -1):
      pdt *= x
      
  return pdt

def dfs(x,depth,k):
  sl[x] = 1
  dl[depth] += 1
  child = 0
  ans = 1
  for i in tree[x]:
    if sl[i] == 0:
      ans *= dfs(i,depth+1,k)
      ans %= mod
      child += 1
  if depth == 0:
    ans *= perm(k-1,child)
    ans %= mod
  else:    
    ans *= perm(k-2,child)
    ans %= mod
  return ans

print(dfs(0,0,k)*k%mod)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import math
n,k = map(int,input().split())
sl = [0]*n
dl = [0]*n
tree = [[] for i in range(n)]
mod = 10**9+7

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

def perm(n, r):
    return math.factorial(n) // math.factorial(n - r)

def dfs(x,depth,k):
  sl[x] = 1
  dl[depth] += 1
  child = 0
  ans = 1
  for i in tree[x]:
    if sl[i] == 0:
      ans *= dfs(i,depth+1,k)
      ans %= mod
      child += 1
  if depth == 0:
    ans *= perm(k-1,child)
    ans %= mod
  else:
    ans *= perm(k-2,child)
    ans %= mod
  return ans

print(dfs(0,0,k)*k%mod)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
mod = 10**9+7

def main():
  
  n,k = map(int,input().split())
  sl = [0]*(n+1)
  tree = [[] for i in range(n+1)]
  
  for i in range(n-1):
    a,b = map(int,input().split())
    tree[a] += (b,)
    tree[b] += (a,)

  def dfs(x,depth,k):
    sl[x] = 1
    child = 0
    ans = 1
    for i in tree[x]:
      if sl[i] == 0:
        ans *= dfs(i,depth+1,k)
        child += 1
    if depth == 0:
      for x in range(k-1, k-1-child, -1):
        ans = ans * x % mod   
    else:    
      for x in range(k-2, k-2-child, -1):
        ans = ans * x % mod
    ans %= mod
    return ans

  print(dfs(1,0,k)*k%mod)

if __name__ == '__main__':
    main()