Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

def per(a, b):
    t=(a,b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re *= a - i
    memo[t]=re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko)
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re *= dfs(ki, i)
    return re%md

md = 10 ** 9 + 7
memo={}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))


Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict

def per(a, b):
    re = 1
    for i in range(b):
        re *= a - i
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko)
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re *= dfs(ki, i)
    return re%md

md = 10 ** 9 + 7
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))


Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
def f(n,k):
    to = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        to[a] += [b]
        to[b] += [a]
    ans = 1
    fin = [0] * (n + 1)
    nd = [1]
    while nd:
        nnd = []
        for i in nd:
            nk = k
            for i1 in to[i]:
                if fin[i1] == 0:
                    nnd.append(i1)
                else:
                    nk -= 1
                nk-=sum(fin[i2] for i2 in to[i1] if i2!=i)
            fin[i] = 1
            ans = ans * nk % md
        nd = nnd
    print(ans)
md=10**9+7
n,k=map(int,input().split())
f(n,k)


Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
def f(n,k):
    to = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        to[a] += [b]
        to[b] += [a]
    ans = 1
    fin = [0] * (n + 1)
    nd = [1]
    while nd:
        nnd = []
        for i in nd:
            nk = k
            for i1 in to[i]:
                if fin[i1] == 0:
                    nnd.append(i1)
                else:
                    nk -= 1
                for i2 in to[i1]:
                    if i2 == i: continue
                    nk -= fin[i2]
            fin[i] = 1
            ans = ans * nk % md
        nd = nnd
    print(ans)
md=10**9+7
n,k=map(int,input().split())
f(n,k)


Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
md=10**9+7
n,k=map(int,input().split())
to=defaultdict(list)
for _ in range(n-1):
    a,b=map(int,input().split())
    to[a]+=[b]
    to[b]+=[a]
ans=1
fin=[0]*(n+1)
nd=[1]
while nd:
    nnd=[]
    for i in nd:
        nk = k
        for i1 in to[i]:
            if fin[i1]==0:
                nnd.append(i1)
            else:
                nk-=1
            for i2 in to[i1]:
                if i2==i:continue
                nk-=fin[i2]
        fin[i]=1
        ans=ans*nk%md
    nd=nnd
print(ans)

Fixed submission:
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)

def per(a, b):
    t = (a, b)
    if t in memo:
        return memo[t]
    re = 1
    for i in range(b):
        re = re * (a - i) % md
    memo[t] = re
    return re

def dfs(i, oi):
    cnt_ko = len(to[i])
    if i == 1:
        re = k * per(k - 1, cnt_ko) % md
    elif cnt_ko == 1:
        return 1
    else:
        re = per(k - 2, cnt_ko - 1)
    for ki in to[i]:
        if ki == oi: continue
        re = re * dfs(ki, i) % md
    return re

md = 10 ** 9 + 7
memo = {}
n, k = map(int, input().split())
to = defaultdict(list)
for _ in range(n - 1):
    a, b = map(int, input().split())
    to[a] += [b]
    to[b] += [a]
print(dfs(1, 0))