Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N - 1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)


def f(root):
    v = K
    stack = [(None, None, root, K - 1)]
    while stack:
        grand_parent, parent, current, k = stack.pop()

        i = 0
        for child in tree[current]:
            if child is parent: continue
            v = v * (k - i) % P
            stack.append((parent, current, child, K - 2))
            i += 1
    return v


print(f(AB[0][0]))

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N - 1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)


def f(root):
    stack = [(None, None, root, K - 1)]
    v = K
    while stack:
        grand_parent, parent, current, k = stack.pop()

        for i, child in enumerate(tree[current] - {parent}):
            v = v * (k - i) % P
            stack.append((parent, current, child, K - 2))
    return v


print(f(AB[0][0]) if AB else K)

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N-1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)

root = AB[0][0]
def f(root):
    v = K
    stack = [(None, None, root, K - 1)]
    while stack:
        grand_parent, parent, current, k = stack.pop()

        for i, child in enumerate(tree[current] - {parent}):
            v = v*(k-i) % P
            stack.append( (parent, current, child, K - 2) )
    return v

print(f(root)) 


Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N - 1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)


def f(root):
    stack = [(None, None, root, K - 1)]
    v = K
    while stack:
        grand_parent, parent, current, k = stack.pop()

        for i, child in enumerate(tree[current] - {parent}):
            v = v * (k - i) % P
            stack.append((parent, current, child, K - 2))
    return v


print(f(AB[0][0]) if AB else K)

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N-1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)

root = AB[0][0]
import sys
sys.setrecursionlimit(10**6)
def f(grand_parent, parent, current, v):
    if parent is None:
        k = K - 1
    else:
        k = K - 2

    for i, child in enumerate(tree[current] - {parent}):
        v = f(parent, current, child, v*(k-i) % P)
    return v

print(f(None, None, root, K)) 


Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
AB = IM(N - 1)

P = 1000000007

tree = {}
for a, b in AB:
    tree.setdefault(a, set()).add(b)
    tree.setdefault(b, set()).add(a)


def f(root):
    stack = [(None, None, root, K - 1)]
    v = K
    while stack:
        grand_parent, parent, current, k = stack.pop()

        for i, child in enumerate(tree[current] - {parent}):
            v = v * (k - i) % P
            stack.append((parent, current, child, K - 2))
    return v


print(f(AB[0][0]) if AB else K)