Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [set() for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
        
    checked = set()
    q = {1}
    res = 1
    while q:
        n = q.pop()
        q.update(set.difference(N_1[n], checked))
        N_2 = set.union(N_1[n], *[N_1[m] for m in N_1[n]]) - {n}
        fixed = len(set.intersection(N_2, checked))
        res *= K - fixed
        checked.add(n)
        if res < 0:
            return str(0)
        else:
            res %= 10**9+7
    return str(res)


if __name__ == '__main__':
    print(solve())



Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [{1},{0}]+[set() for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
    checked, q = set(), {0}
    res = 1
    while q:
        n = q.pop()
        checked.add(n)
        q.update(set.difference(N_1[n], checked))
        K_ = K-min(2,n)
        for i in range(len(N_1[n])-(n>0)):
            if K_ == i:
                return str(0)
            res = (res*(K_-i))%(10**9+7)
    return str(res)
if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [set() for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
        
    checked = set()
    q = {1}
    res = 1
    while q:
        n = q.pop()
        q |= N_1[n] - checked
        fixed = len(set.union(*(N_1[m] for m in N_1[n]), N_1[n]-{n}) & checked)
        res *= K - fixed
        checked.add(n)
        if res < 0:
            return str(0)
        else:
            res %= 10**9+7
    return str(res)


if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [{1},{0}]+[set() for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
    checked, q = set(), {0}
    res = 1
    while q:
        n = q.pop()
        checked.add(n)
        q.update(set.difference(N_1[n], checked))
        K_ = K-min(2,n)
        for i in range(len(N_1[n])-(n>0)):
            if K_ == i:
                return str(0)
            res = (res*(K_-i))%(10**9+7)
    return str(res)
if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [set() for _ in range(N+1)]
    N_2 = [set() for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
    for n in range(1, N+1):
        for m in N_1[n]:
            N_2[m] |= N_1[n]-{m}|{n}
        
    checked = set()
    q = {1}
    res = 1
    while q:
        n = q.pop()
        q |= N_1[n] - checked
        res *= K - len(N_2[n] & checked)
        checked.add(n)
        if res < 0:
            return str(0)
        else:
            res %= 10**9+7
    return str(res)


if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [{1},{0}]+[set() for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
    checked, q = set(), {0}
    res = 1
    while q:
        n = q.pop()
        checked.add(n)
        q.update(set.difference(N_1[n], checked))
        K_ = K-min(2,n)
        for i in range(len(N_1[n])-(n>0)):
            if K_ == i:
                return str(0)
            res = (res*(K_-i))%(10**9+7)
    return str(res)
if __name__ == '__main__':
    print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [set() for _ in range(N+1)]
    N_2 = [set() for _ in range(N+1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_2[a] |= N_1[b] | {b}
        N_2[b] |= N_1[a] | {a}
        for n in N_1[b]:
            N_2[n].add(a)
        for n in N_1[a]:
            N_2[n].add(b)
        N_1[a].add(b)
        N_1[b].add(a)
    checked = set()
    q = {1}
    res = 1
    while q:
        n = q.pop()
        q |= N_1[n] - checked
        res *= K - len(N_2[n] & checked)
        checked.add(n)
        if res < 0:
            return str(0)
        else:
            res %= 10**9+7
    return str(res)


if __name__ == '__main__':
    print(solve())


Fixed submission:
# -*- coding: utf-8 -*-
def solve():
    N, K = map(int, input().split())
    N_1 = [{1},{0}]+[set() for _ in range(N-1)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        N_1[a].add(b)
        N_1[b].add(a)
    checked, q = set(), {0}
    res = 1
    while q:
        n = q.pop()
        checked.add(n)
        q.update(set.difference(N_1[n], checked))
        K_ = K-min(2,n)
        for i in range(len(N_1[n])-(n>0)):
            if K_ == i:
                return str(0)
            res = (res*(K_-i))%(10**9+7)
    return str(res)
if __name__ == '__main__':
    print(solve())