Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue

        ans = (ans*f(k-2)//f(k-2-len(edge[i])+1))%bign
    print(ans)


main()


Fixed submission:
from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue
        t = k-2
        for j in range(len(edge[i])-1):
            ans = ans * t % bign
            t-=1
        
    print(ans)


main()

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue

        ans = (ans*f(k-2)//f(k-2-len(edge[i])+1))%bign
    print(ans)


main()


Fixed submission:
from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue
        t = k-2
        for j in range(len(edge[i])-1):
            ans = ans * t % bign
            t-=1
        
    print(ans)


main()

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial as f
from sys import exit


bign = 10**9+7
n,k = map(int,input().split())

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

if len(edge[1]) > k-1:
    print(0)
    exit()
ans = (f(k)//f(k-1-len(edge[1])))%bign

for i in range(2,n+1):
    if len(edge[i]) > k-1:
        print(0)
        exit()
    if len(edge[i])==1:
        continue

    ans = (ans*f(k-2)//f(k-2-len(edge[i])+1))%bign
print(ans)





Fixed submission:
from math import factorial as f
from sys import exit

def main():
    bign = 10**9+7
    n,k = map(int,input().split())

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

    if len(edge[1]) > k-1:
        print(0)
        exit()
    ans = (f(k)//f(k-1-len(edge[1])))%bign

    for i in range(2,n+1):
        if len(edge[i]) > k-1:
            print(0)
            exit()
        if len(edge[i])==1:
            continue
        t = k-2
        for j in range(len(edge[i])-1):
            ans = ans * t % bign
            t-=1
        
    print(ans)


main()