Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 7)


def LI(): return list(map(int, stdin.readline().split()))


inf = 1000000007


def main():
    N, K = LI()
    graph = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = LI()
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)

    stack = [0]
    ans = 1
    done = [False] * N
    while len(stack) > 0:
        now = stack.pop(0)
        done[now] = True
        cnt = 0
        for x in graph[now]:
            if not done[x]:
                cnt += 1
                stack.append(x)
        if now == 0:
            for i in range(cnt):
                ans = (K - 1 - i) * ans % inf
        else:
            for i in range(cnt):
                ans = (K - 2 - i) * ans % inf
    print(ans * K % inf)


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 7)


def LI(): return list(map(int, stdin.readline().split()))


inf = 1000000007


def main():
    N, K = LI()
    graph = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = LI()
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)

    stack = [0]
    ans = 1
    done = [False for _ in range(N)]
    while len(stack) > 0:
        now = stack.pop(0)
        done[now] = True
        cnt = 0
        for x in graph[now]:
            if not done[x]:
                cnt += 1
                stack.append(x)
        if now == 0:
            for i in range(cnt):
                ans = (K - 1 - i) * ans % inf
        else:
            for i in range(cnt):
                ans = (K - 2 - i) * ans % inf
    print(ans * K % inf)


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin


def LI(): return list(map(int, stdin.readline().split()))


inf = 1000000007


def main():
    N, K = LI()
    graph = [[] for _ in range(N)]
    for i in range(N - 1):
        a, b = LI()
        graph[a - 1].append(b - 1)
        graph[b - 1].append(a - 1)

    stack = [0]
    ans = 1
    done = [False for _ in range(N)]
    while len(stack) > 0:
        now = stack.pop(0)
        done[now] = True
        cnt = 0
        for x in graph[now]:
            if not done[x]:
                cnt += 1
                stack.append(x)
        if now == 0:
            for i in range(cnt):
                ans = (K - 1 - i) * ans % inf
        else:
            for i in range(cnt):
                ans = (K - 2 - i) * ans % inf
    print(ans * K % inf)


if __name__ == "__main__":
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin


def LI(): return list(map(int, stdin.readline().split()))


inf = 1000000007

N, K = LI()
graph = [[] for _ in range(N)]
for i in range(N - 1):
    a, b = LI()
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)

stack = [0]
ans = 1
done = [False for _ in range(N)]
cnt = 0
parent = -1
while len(stack) > 0:
    now = stack.pop(0)
    done[now] = True
    cnt = 0
    for x in graph[now]:
        if not done[x]:
            cnt += 1
            stack.append(x)
    if now == 0:
        temp = 1
        for i in range(cnt):
            ans = (K - 1 - i) * ans % inf
    else:
        temp = 1
        for i in range(cnt):
            ans = (K - 2 - i) * ans % inf
print(ans * K % inf)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
ab = [[int(x) - 1 for x in stdin.readline().rstrip().split()] for _ in range(N - 1)]
inf = 1000000007

graph = [[] for _ in range(N)]
for n in range(N - 1):
    graph[ab[n][0]].append(ab[n][1])
    graph[ab[n][1]].append(ab[n][0])

stack = [0]
ans = 1
done = [False for _ in range(N)]
cnt = 0
parent = -1
while len(stack) > 0:
    now = stack.pop(0)
    done[now] = True
    cnt = 0
    for x in graph[now]:
        if not done[x]:
            cnt += 1
            stack.append(x)
    if now == 0:
        temp = 1
        for i in range(cnt):
            ans = (K - 1 - i) * ans % inf
    else:
        temp = 1
        for i in range(cnt):
            ans = (K - 2 - i) * ans % inf
print(ans * K % inf)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
ab = [[int(x) - 1 for x in stdin.readline().rstrip().split()] for _ in range(N - 1)]
inf = 1000000007

graph = [[] for _ in range(N)]
for n in range(N - 1):
    graph[ab[n][0]].append(ab[n][1])
    graph[ab[n][1]].append(ab[n][0])

stack = [(0, -1)]
ans = 1
done = [False for _ in range(N)]
cnt = 0
parent = -1
while len(stack) > 0:
    now = stack.pop(0)
    done[now[0]] = True
    for x in graph[now[0]]:
        if not done[x]:
            stack.append((x, now[0]))
    if now[0] == 0:
        ans = K * ans % inf
    else:
        if parent != now[1]:
            cnt = 0
        else:
            cnt += 1
        parent = now[1]
        if parent == 0:
            ans = (K - 1 - cnt) * ans % inf
        else:
            ans = (K - 2 - cnt) * ans % inf
print(ans % inf)


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()