Buggy python submission (verdict: Time Limit Exceeded):
[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for i in range(0, N):
        j = i + 1
        c = Es.count(j)
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1)

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

Es_count = [len(list(g[1])) for g in groupby(sorted(Es))]
res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es_count[i]
    if c > K-1:
        res = 0
        break
    res *= num_of_perms[c-1]
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= num_of_perms[c-1]
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1)

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

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= num_of_perms[c-1]
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
def num_of_perms(n, r):
    if r == 0:
        return 1
    else:
        return (n-r+1) * num_of_perms(n, r-1)

[N, K] = map(int, input().split())
Es = []
for _ in range(0, N-1):
    [a, b] = input().split()
    Es.append(int(a))
    Es.append(int(b))

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= num_of_perms((K-2), (c-1))
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
import numpy as np

[N, K] = map(int, input().split())
num_of_perms = np.ones((K-1, K-1))
for n in range(1, K-1):
    for j in range(1, n):
        i = n - j
        num_of_perms[i][j] = num_of_perms[i-1][j] + num_of_perms[i][j-1]

[a1, b1] = map(int, input().split())
Es = [a1, b1]
for _ in range(1, N-1):
    [a, b] = map(int, input().split())
    Es.append(a)
    Es.append(b)

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= int(num_of_perms[(K-2)-(c-1)][c-1])
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
def num_of_perms(n, r):
    p = 1
    for i in range(int((n+1)/2)):
        if i < r and r < n - i:
            p *= (n - i)
    return p

[N, K] = map(int, input().split())
[a1, b1] = map(int, input().split())
Es = [a1, b1]
for _ in range(1, N-1):
    [a, b] = map(int, input().split())
    Es.append(a)
    Es.append(b)

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= num_of_perms((K-2), (c-1))
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
from itertools import permutations

[N, K] = map(int, input().split())
[a1, b1] = map(int, input().split())
Es = [a1, b1]
for _ in range(1, N-1):
    [a, b] = map(int, input().split())
    Es.append(a)
    Es.append(b)

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= len(list(permutations(range(K-2), (c-1))))
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)

Buggy python submission (verdict: Runtime Error):
from itertools import *

[N, K] = map(int, input().split())
[a1, b1] = map(int, input().split())
print([a1, b1])
Es = [a1, b1]
for _ in range(1, N-1):
    [a, b] = map(int, input().split())
    Es.append(a)
    Es.append(b)

res = K * (K-1)
res %= 1000000007
for i in range(0, N):
    j = i + 1
    c = Es.count(j)
    if c > K-1:
        res = 0
        break
    res *= len(list(permutations(range(K-2), (c-1))))
    res %= 1000000007

print(res)


Fixed submission:
from itertools import groupby

[N, K] = map(int, input().split())

num_of_perms = [1] * (K-1)
for i in range(1, K-1):
    num_of_perms[i] = num_of_perms[i-1] * ((K - 2) - i + 1) % 1000000007

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

if N == 1:
    res = K
else:
    res = K * (K-1)
    res %= 1000000007
    for g in groupby(sorted(Es)):
        c = len(list(g[1]))
        if c > K-1:
            res = 0
            break
        res *= num_of_perms[c-1]
        res %= 1000000007

print(res)