Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
info=[[False,set(),set(),-1] for _ in [0]*N]
q=[[0,""]]
while(q):
    i,j = q.pop()
    if j!="":
        info[i][1].add(j)
        [info[i][2].add(k) for k in info[j][1]]
    if info[i][0]==False:
        info[i][0]=True
        info[i][3]=K-len(info[i][1])-len(info[i][2])
    while(graph[i]):
        g=graph[i].pop()
        q.append((g,i))
ans=1
mod=10**9+7
for i in info:
    ans=(ans*i[3])%mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
[graph[a-1].append(b-1) for a,b in ab]
[graph[b-1].append(a-1) for a,b in ab]
q=[[-1,0]]
cnt=0
ans=K
mod=10**9+7
while(q):
    q1=[]
    if cnt<2:
        cnt+=1
    for i,j in q:
        g=graph[j]
        if i!=-1:
            g.remove(i)
        for cnt2,k in enumerate(g):
            q1.append([j,k])
            ans=(ans*(K-cnt-cnt2))%mod
    q=q1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
info=[[False,set(),set(),-1] for _ in [0]*N]
q=[[0,""]]
while(q):
    i,j = q.pop()
    if j!="":
        info[i][1].add(j)
        [info[i][2].add(k) for k in info[j][1]]
    if info[i][0]==False:
        info[i][0]=True
        info[i][3]=K-len(info[i][1])-len(info[i][2])
    while(graph[i]):
        g=graph[i].pop()
        q.append((g,i))
ans=1
mod=10**9+7
for i in info:
    ans=(ans*i[3])%mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
[graph[a-1].append(b-1) for a,b in ab]
[graph[b-1].append(a-1) for a,b in ab]
q=[[-1,0]]
cnt=0
ans=K
mod=10**9+7
while(q):
    q1=[]
    if cnt<2:
        cnt+=1
    for i,j in q:
        g=graph[j]
        if i!=-1:
            g.remove(i)
        for cnt2,k in enumerate(g):
            q1.append([j,k])
            ans=(ans*(K-cnt-cnt2))%mod
    q=q1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
info=[[False,set(),set(),-1] for _ in [0]*N]
q=[[0,""]]
while(q):
    i,j = q.pop()
    if j!="":
        info[i][1].add(j)
        info[i][2]|={k for k in info[j][1]}
    if info[i][0]==False:
        info[i][0]=True
        info[i][3]=K-len(info[i][1])-len(info[i][2])
    while(graph[i]):
        g=graph[i].pop()
        q.append((g,i))
ans=1
mod=10**9+7
for i in info:
    ans=(ans*i[3])%mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
[graph[a-1].append(b-1) for a,b in ab]
[graph[b-1].append(a-1) for a,b in ab]
q=[[-1,0]]
cnt=0
ans=K
mod=10**9+7
while(q):
    q1=[]
    if cnt<2:
        cnt+=1
    for i,j in q:
        g=graph[j]
        if i!=-1:
            g.remove(i)
        for cnt2,k in enumerate(g):
            q1.append([j,k])
            ans=(ans*(K-cnt-cnt2))%mod
    q=q1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
    
info=[[False,set(),set(),-1] for _ in [0]*N]
q=[[0,""]]
status=[set(),set()]
while(q):
    i,j = q.pop()
    if j!="":
        status=[{j},{k for k in info[j][1]}]
    info[i][1]=status[0]|info[i][1]
    info[i][2]=status[1]|info[i][2]
    if info[i][0]==False:
        info[i][0]=True
        info[i][3]=K-len(info[i][1])-len(info[i][2])
    while(graph[i]):
        g=graph[i].pop()
        q.append((g,i))
ans=1
mod=10**9+7
for i in info:
    ans=(ans*i[3])%mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
[graph[a-1].append(b-1) for a,b in ab]
[graph[b-1].append(a-1) for a,b in ab]
q=[[-1,0]]
cnt=0
ans=K
mod=10**9+7
while(q):
    q1=[]
    if cnt<2:
        cnt+=1
    for i,j in q:
        g=graph[j]
        if i!=-1:
            g.remove(i)
        for cnt2,k in enumerate(g):
            q1.append([j,k])
            ans=(ans*(K-cnt-cnt2))%mod
    q=q1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
for a,b in ab:
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
    
info=[[False,set(),set(),-1] for _ in [0]*N]
q=[[0,""]]
status=[set(),set()]
while(q):
    i,j = q.pop()
    if j!="":
        status=[{j},{k for k in info[j][1]}]
    info[i][1]=status[0]|info[i][1]
    info[i][2]=status[1]|info[i][2]
    if info[i][0]==False:
        info[i][0]=True
        info[i][3]=K-len(info[i][1])-len(info[i][2])
    while(graph[i]):
        g=graph[i].pop()
        q.append((g,i))
ans=1
mod=10**9+7
for i in info:
    ans=(ans*i[3])%mod
print(ans)

Fixed submission:
N,K=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(N-1)]
graph=[[] for _ in [0]*N]
[graph[a-1].append(b-1) for a,b in ab]
[graph[b-1].append(a-1) for a,b in ab]
q=[[-1,0]]
cnt=0
ans=K
mod=10**9+7
while(q):
    q1=[]
    if cnt<2:
        cnt+=1
    for i,j in q:
        g=graph[j]
        if i!=-1:
            g.remove(i)
        for cnt2,k in enumerate(g):
            q1.append([j,k])
            ans=(ans*(K-cnt-cnt2))%mod
    q=q1
print(ans)