Problem p02985 (python)

Fixed submission:
mo=10**9+7
n,k=map(int,input().split())

def P(n,k,mo):
    rt=1
    for i in range(k):
        rt*=(n-i)
        rt%=mo
    return rt

g=[[] for i in range(1+n)]
v=[0]*(1+n)

for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)

def dfs(cur):
    q=[]
    q.append(cur)

    while q:
        cur=q.pop()
        v[cur]=1
        c=len(g[cur])-1
        ans[0]*=P(k-2,c,mo)
        
        for nb in g[cur]:
            if v[nb]!=1:
                q.append(nb)
v[1]=1
ans=[P(k,1+len(g[1]),mo)]

for j in g[1]:
    dfs(j)
print(ans[0]%mo)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7

n,k=map(int,input().split())

def P(n,k,mo):
    rt=1
    for i in range(k):
        rt*=(n-i)
        rt%=mo
    return rt

g=[[] for i in range(1+n)]
v=[0]*(1+n)
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)

def dfs(cur):
    c=len(g[cur])-1 #sum([v[i]!=1 for i in g[cur]])
    if c==0:
        return
    v[cur]=1
    ans[0]*=P(k-2,c,mo)
    for nb in g[cur]:
        if v[nb]!=1:
            dfs(nb)
v[1]=1
ans=[P(k,1+len(g[1]),mo)]

for j in g[1]:
    c=sum([v[i]!=1 for i in g[j]])
    ans*=P(k-2,c,mo)
    dfs(j)
print(ans[0]%mo)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7

n,k=map(int,input().split())

def P(n,k,mo):
    rt=1
    for i in range(k):
        rt*=(n-i)
        rt%=mo
    return rt

g=[[] for i in range(1+n)]
v=[0]*(1+n)
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)

def dfs(cur):
    c=len(g[c])-1 #sum([v[i]!=1 for i in g[cur]])
    if c==0:
        return
    v[cur]=1
    ans[0]*=P(k-2,c,mo)
    for nb in g[cur]:
        if v[nb]!=1:
            dfs(nb)
v[1]=1
ans=[P(k,1+len(g[1]),mo)]

for j in g[1]:
    c=sum([v[i]!=1 for i in g[j]])
    ans*=P(k-2,c,mo)
    dfs(j)
print(ans[0]%mo)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7

n,k=map(int,input().split())

def P(n,k,mo):
    rt=1
    for i in range(k):
        rt*=(n-i)
        rt%=mo
    return rt

g=[[] for i in range(1+n)]
v=[0]*(1+n)
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)

def dfs(cur):
    c=sum([v[i]!=1 for i in g[cur]])
    if c==0:
        return
    v[cur]=1
    ans[0]*=P(k-2,c,mo)
    for nb in g[cur]:
        if v[nb]!=1:
            dfs(nb)
v[1]=1
ans=[P(k,1+len(g[1]),mo)]

for j in g[1]:
    c=sum([v[i]!=1 for i in g[j]])
    ans*=P(k-2,c,mo)
    dfs(j)
print(ans[0]%mo)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7

n,k=map(int,input().split())

f=[0]*(1+k)

def P(n,k,mo):
    rt=1
    for i in range(k):
        rt*=(n-i)
        rt%=mo
    return rt

g=[[] for i in range(1+n)]
v=[0]*(1+n)
for i in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)

ans=[k]
print(g)
def dfs(cur):
    c=sum([v[i]!=1 for i in g[cur]])
    print(cur,c,ans,P(k-2,c,mo))
    if c==0:
        return
    v[cur]=1
    ans[0]*=P(k-2,c,mo)
    for nb in g[cur]:
        if v[nb]!=1:
            dfs(nb)
v[1]=1
for j in g[1]:
    c=sum([v[i]!=1 for i in g[j]])
    ans*=P(k-1,c,mo)
    dfs(j)
print(ans[0]%mo)