Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque


def solve():
    N, K = map(int, input().split())
    d = defaultdict(list)
    MOD = 10**9+7

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

    ans = K
    used = [0]*(N+1)
    used[1] = True
    q = deque([1])

    while q:
        a = q.popleft()
        s = 1 if a==1 else 2
        for b in d[a]:
            if used[b]:continue
            used[b] = True
            ans *= (K-s)
            s += 1
            q.append(b)

        ans %= MOD
    print(ans)

solve()

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque


def solve():
    N, K = map(int, input().split())
    d = defaultdict(list)
    MOD = 10**9+7

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

    ans = K
    used = [0]*(N+1)
    used[1] = True
    q = deque([1])

    while q:
        a = q.popleft()
        s = 1 if a==1 else 2
        for b in d[a]:
            if used[b]:continue
            used[b] = True
            ans *= (K-s)
            s += 1
            q.append(b)

    ans %= MOD
    print(ans)

solve()

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict, deque

N, K = map(int, input().split())
d = defaultdict(list)
MOD = 10**9+7

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

ans = K
used = [0]*(N+1)
used[1] = True
q = deque([1])

while q:
    s = 1 if a==1 else 2
    for b in d[a]:
        if used[b]:continue
        used[b] = True
        ans *= (K-s)
        s += 1
        q.append(b)

ans %= MOD
print(ans)


Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict, deque

N, K = map(int, input().split())
d = defaultdict(list)
MOD = 10**9+7

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

ans = K
used = [0]*(N+1)
used[1] = True
q = deque([1])

while q:
    a = q.popleft()
    s = 1 if a==1 else 2
    for b in d[a]:
        if used[b]:continue
        used[b] = True
        ans *= (K-s)
        s += 1
        q.append(b)
	ans %= MOD

print(ans%MOD)


Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from functools import reduce
import sys
sys.setrecursionlimit(1000000)

v = defaultdict(list)
mod = 10**9+7

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

v = dict(v)
visited = [False]*(N+1)
p = [0]*(N+1)

def dfs(pos, parent, score):
    visited[pos] = True
    p[pos] = K - score
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        dfs(i, pos, a+b)
        b += 1

dfs(1, 0, 0)

ans = p[1]
for i in p[2:]:
    ans *= i
    ans %= mod

print(ans)

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from functools import reduce
import sys
sys.setrecursionlimit(1000000)

v = defaultdict(list)
mod = 10**9+7

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

v = dict(v)
visited = [False]*(N+1)
p = [0]*(N+1)

def dfs(pos, parent, score):
    visited[pos] = True
    p[pos] = K - score
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        dfs(i, pos, a+b)
        b += 1

dfs(1, 0, 0)
ans = reduce(lambda x,y:(x*y)%mod, p[1:])
print(ans)

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from functools import reduce
import sys
sys.setrecursionlimit(1000000)

v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
p = [0]*(N+1)

def dfs(pos, parent, score):
    visited[pos] = True
    p[pos] = K - score
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        dfs(i, pos, a+b)
        b += 1

dfs(1, 0, 0)
ans = reduce(lambda x,y:(x*y)%mod, p[1:])
print(ans)

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from functools import reduce
import sys
sys.setrecursionlimit(1000000)

v = defaultdict(list)
p = defaultdict(int)
mod = 10**9+7

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

visited = [False]*(N+1)

def dfs(pos, parent, score):
    visited[pos] = True
    p[pos] = K - score
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        dfs(i, pos, a+b)
        b += 1
dfs(1, 0, 0)

ans = reduce(lambda x,y:(x*y)%mod, p.values())
print(ans)

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from functools import reduce
import sys
sys.setrecursionlimit(1000000)

v = defaultdict(list)
p = defaultdict(int)
mod = 10**9+7

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

visited = [False]*(N+1)
def dfs(pos, parent):
    visited[pos] = True
    p[pos] = K-sum(visited[i] for i in v[parent])
    for i in v[pos]:
        if visited[i]:continue
        visited[i] = True
        dfs(i, pos)

dfs(1, 0)
ans = reduce(lambda x,y:(x*y)%mod, p.values())
print(ans)

Fixed submission:
from collections import defaultdict
v = defaultdict(list)
mod = 10**9+7

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

visited = [False]*(N+1)
ans = 1

q = [(1, 0, 0)]
while q:
    pos, parent, score = q.pop()
    visited[pos] = True
    ans *= (K-score)
    ans %= mod 
   
    a = 1 if pos==1 else 2
    b = 0

    for i in v[pos]:
        if visited[i]:continue
        q.append((i, pos, a+b))
        b += 1

print(ans)