Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [[-1,-1] for _ in [0]*N]
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        tmp = sorted(set(V[i]))
        cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = len(cSet)
        for j in E[i]:
            if V[j][0]!=-1:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
            l -= 1
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [-1]*N
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        tmp = sorted(set(V[i]))
        cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = len(cSet)
        for j in E[i]:
            if V[j]!=-1:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
            l -= 1
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [-1]*N
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        tmp = sorted(set(V[i]))
        cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        for j in E[i]:
            if V[j]!=-1:continue
            ans = ans * len(cSet) % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [-1]*N
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        tmp = sorted(set(V[i]))
        cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        for j in E[i]:
            if V[j]!=-1:continue
            ans = ans * len(cSet) % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [-1]*N
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        cSet = set(range(K))-set(V[i])
        for j in E[i]:
            if V[j]!=-1:continue
            ans = ans * len(cSet) % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [-1]*N
V[0] = [0,0]
while q:
    qq = []
    for i in q:
        cSet = set(range(K))-set(V[i])
        for j in E[i]:
            if V[j]!=-1:continue
            ans = ans * len(cSet) % mod
            if ans == 0:break
            qq.append(j)
            V[j] = [cSet.pop(),V[i][0]]
    q = qq
print(ans)

Fixed submission:
N,K = map(int,input().split())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
mod = 10**9+7

E = [[] for _ in [0]*N]
for a,b in AB:
    E[a-1].append(b-1)
    E[b-1].append(a-1)

ans = K
q = [0]
V = [False for _ in [0]*N]
V[0] = True
while q:
    qq = []
    for i in q:
        #tmp = sorted(set(V[i]))
        #cSet = list(range(tmp[0])) + list(range(tmp[0]+1,tmp[-1])) + list(range(tmp[-1]+1,K))
        l = K-2+(i==0)
        for j in E[i]:
            if V[j]:continue
            ans = ans * l % mod
            if ans == 0:break
            qq.append(j)
            V[j] = True
            l -= 1
    q = qq
print(ans)