Buggy python submission (verdict: Time Limit Exceeded):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

# G = [set() for _ in range(N)]
G = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)


def len12(g, st):
    s = []
    for n1 in g[st]:
        s.append(n1)
        for n2 in g[n1]:
            if n2 != st:
                s.append(n2)
    return len(s)


G2 = [[] for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].append(n)
        G2[n].append(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)


Fixed submission:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

# G = [set() for _ in range(N)]
G = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)


def len12(g, st):
    cnt = len(g[st])
    for n1 in g[st]:
        cnt += (len(g[n1]) - 1)
    return cnt


G2 = [[] for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].append(n)
        G2[n].append(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

G = [set() for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].add(b)
    G[b].add(a)


def len12(g, st):
    s = set()
    for n1 in g[st]:
        s.add(n1)
        for n2 in g[n1]:
            s.add(n2)
    if st in s:
        s.remove(st)
    return len(s)


G2 = [set() for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].add(n)
        G2[n].add(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)


Fixed submission:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

# G = [set() for _ in range(N)]
G = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)


def len12(g, st):
    cnt = len(g[st])
    for n1 in g[st]:
        cnt += (len(g[n1]) - 1)
    return cnt


G2 = [[] for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].append(n)
        G2[n].append(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

G = [set() for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].add(b)
    G[b].add(a)


def len12(g, st):
    s = set()
    for n1 in g[st]:
        s.add(n1)
        for n2 in g[n1]:
            s.add(n2)
    if st in s:
        s.remove(st)
    return len(s)


G2 = [set() for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].add(n)
        G2[n].add(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)


Fixed submission:
import sys, math, itertools, bisect, copy, re
from collections import Counter, deque, defaultdict

# from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
# from functools import reduce
# from math import ceil, floor, log10, log2, factorial
# from pprint import pprint

INF = float('inf')
MOD = 10 ** 9 + 7
EPS = 10 ** -7
sys.setrecursionlimit(1000000)

# N = int(input())
# N,M = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]
# DP = [[0] * 100 for _ in range(100)]
# DP = defaultdict(lambda: float('inf'))


N, K = [int(x) for x in input().split()]

# G = [set() for _ in range(N)]
G = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)


def len12(g, st):
    cnt = len(g[st])
    for n1 in g[st]:
        cnt += (len(g[n1]) - 1)
    return cnt


G2 = [[] for _ in range(N)]
opened = set()
opened.add(0)
closed = set()

ans = K
while len(opened) != 0:
    target = opened.pop()
    closed.add(target)
    for n in G[target]:
        if n in closed:
            continue
        opened.add(n)
        G2[target].append(n)
        G2[n].append(target)
        cnt = len12(G2, n)
        ans = ans * (K - cnt)
        ans %= MOD

print(ans)