Problem p02985 (python)

Fixed submission:
import sys
sys.setrecursionlimit(410000)

mod = 10 ** 9 + 7
def dfs(v, p, k):
    ret = 1
    if len(graph[v]) > k:
        return 0
    kc = k - 2
    if p == -1:
        kc = k - 1
    for vi in graph[v]:
        if vi != p:
            ret *= kc
            ret = ret % mod
            kc -= 1
    for vi in graph[v]:
        if vi != p:
            ret *= dfs(vi, v, k)
            ret = ret % mod
    return ret

n, k = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    
print((dfs(0, -1, k) * k) % mod)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
def dfs(v, p, k):
    ret = 1
    if len(graph[v]) > k:
        return 0
    kc = k - 2
    if p == -1:
        kc = k - 1
    for vi in graph[v]:
        if vi != p:
            ret *= kc
            ret = ret % mod
            kc -= 1
    for vi in graph[v]:
        if vi != p:
            ret *= dfs(vi, v, k)
            ret = ret % mod
    return ret

n, k = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    
print((dfs(0, -1, k) * k) % mod)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
def dfs(v, p, k):
    ret = 1
    if len(graph[v]) > k:
        return 0
    kc = k - 2
    if p == -1:
        kc = k - 1
    for vi in graph[v]:
        if vi != p:
            ret *= kc
            ret = ret % mod
            kc -= 1
    for vi in graph[v]:
        if vi != p:
            ret *= dfs(vi, v, k)
            ret = ret % mod
    return ret

n, k = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    
print((dfs(0, -1, k) * k) % mod)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
def dfs(v, p, k):
    ret = 1
    if len(graph[v]) > k:
        return 0
    kc = k - 2
    if p == -1:
        kc = k - 1
    for vi in graph[v]:
        if vi != p:
            ret *= kc
            ret = ret % mod
            ret *= dfs(vi, v, k)
            ret = ret % mod
            kc -= 1
        else:
            continue
    return ret

n, k = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    
print((dfs(0, -1, k) * k) % mod)

Buggy submission (verdict: Runtime Error):
mod = 10 ** 9 + 7
def dfs(v, p, k):
    ret = 1
    if len (graph[v]) > k:
        return 0
    kc = k - 2
    if p == -1:
        kc = k - 1
    for vi in graph[v]:
        if vi != p:
            ret *= kc
            ret = ret % mod
            ret *= dfs(vi, v, k)
            ret = ret % mod
            kc -= 1
    return ret

n, k = map(int, input().split())
graph = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    graph[a - 1].append(b - 1)
    graph[b - 1].append(a - 1)
    
print((dfs(0, -1, k) * k) % mod)