Problem p02985 (python)

Fixed submission:
n,k=map(int,input().split())
mod=10**9+7
import sys
sys.setrecursionlimit(10**9)
root=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)
def perm(k,n):
    x=1
    if k-n+1<=0:
        return 0
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,2,node)%mod
    return x%mod
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
mod=10**9+7
root=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)
def perm(k,n):
    x=1
    if k-n+1<=0:
        return 0
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,2,node)%mod
    return x%mod
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
mod=10**9+7
root=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)
def perm(k,n):
    x=1
    if k-n+1<=0:
        return 0
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    return x%mod
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
mod=10**9+7
root=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)
def perm(k,n):
    x=1
    if k-n+1<=0:
        return 0
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    return x
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
edge=[]
mod=10**9+7
for i in range(n-1):
    a,b=map(int,input().split())
    edge.append([a-1,b-1])
root=[[] for i in range(n)]
for [a,b] in edge:
    root[a].append(b)
    root[b].append(a)
def perm(k,n):
    x=1
    if k-n+1<=0:
        return 0
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    return x
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
edge=[]
mod=10**9+7
for i in range(n-1):
    a,b=map(int,input().split())
    edge.append([a-1,b-1])
root=[[] for i in range(n)]
for [a,b] in edge:
    root[a].append(b)
    root[b].append(a)
def perm(k,n):
    x=1
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and depth>1:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    return x
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
edge=[]
mod=10**9+7
for i in range(n-1):
    a,b=map(int,input().split())
    edge.append([a-1,b-1])
root=[[] for i in range(n)]
for [a,b] in edge:
    root[a].append(b)
    root[b].append(a)
def perm(k,n):
    x=1
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    x=1
    if l==0:
        return 1
    if l==1 and root[node][0]==prev:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    return x
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
edge=[]
mod=10**9+7
for i in range(n-1):
    a,b=map(int,input().split())
    edge.append([a-1,b-1])
root=[[] for i in range(n)]
for [a,b] in edge:
    root[a].append(b)
    root[b].append(a)
#print(root)
def perm(k,n):
    x=1
    for i in range(k,k-n,-1):
        x=x*i%mod
    return x
def dfs(node,depth,prev): 
    l=len(root[node])
    d=depth
    if l==1 and root[node][0]==prev:
        return 1
    if depth>1:
        x=perm(k-d,l-1)
    else:
        x=perm(k-d,l)
    #print(node,x,k-d,l)
    for y in root[node]:
        if y!=prev:
            x=x*dfs(y,min(depth+1,2),node)%mod
    #print(node,x)
    return x
count=dfs(0,1,0)*k%mod
print(count if count>0 else 0)