Problem p02985 (python)

Fixed submission:
import sys,heapq,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

def perm(k,d):
    x = k
    for i in range(1,d):
        x = (x * (k-i)) % R
    return x

def dfs(prev, cur):
    d = len(dst[cur])-1
    if d==0:
        return 1
    x = perm(k-2,d)
    for nd in dst[cur]:
        if nd==prev:
            continue
        x = (x * dfs(cur,nd)) % R
    return x

sys.setrecursionlimit(250000)
soft,hard = resource.getrlimit(resource.RLIMIT_STACK)
resource.setrlimit(resource.RLIMIT_STACK,(128*1024*1024,hard))
n,k = inm()
dst = [ [] for i in range(n) ]
for i in range(n-1):
    a,b = inm()
    a -= 1
    b -= 1
    dst[a].append(b)
    dst[b].append(a)

for i in range(n):
    if len(dst[i]) >= k:
        print(0)
        exit()

d = len(dst[0])
x = perm(k,d+1)
for nd in dst[0]:
    x = (x * dfs(0,nd)) % R
print(x)

Buggy submission (verdict: Runtime Error):
import sys,heapq,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

def perm(k,d):
    x = k
    for i in range(1,d):
        x = (x * (k-i)) % R
    return x

def dfs(prev, cur):
    d = len(dst[cur])-1
    if d==0:
        return 1
    x = perm(k-2,d)
    for nd in dst[cur]:
        if nd==prev:
            continue
        x = (x * dfs(cur,nd)) % R
    return x

sys.setrecursionlimit(250000)
soft,hard = resource.getrlimit(resource.RLIMIT_STACK)
resource.setrlimit(resource.RLIMIT_STACK,(210000,hard))
n,k = inm()
dst = [ [] for i in range(n) ]
for i in range(n-1):
    a,b = inm()
    a -= 1
    b -= 1
    dst[a].append(b)
    dst[b].append(a)

for i in range(n):
    if len(dst[i]) >= k:
        print(0)
        exit()

d = len(dst[0])
x = perm(k,d+1)
for nd in dst[0]:
    x = (x * dfs(0,nd)) % R
print(x)

Buggy submission (verdict: Runtime Error):
import sys,heapq,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

def perm(k,d):
    x = k
    for i in range(1,d):
        x = (x * (k-i)) % R
    return x

def dfs(prev, cur):
    d = len(dst[cur])-1
    if d==0:
        return 1
    x = perm(k-2,d)
    for nd in dst[cur]:
        if nd==prev:
            continue
        x = (x * dfs(cur,nd)) % R
    return x

sys.setrecursionlimit(250000)
soft,hard = resource.getrlimit(resource.RLIMIT_STACK)
resource.setrlimit(resource.RLIMIT_STACK,(210000,hard))
n,k = inm()
dst = [ [] for i in range(n) ]
for i in range(n-1):
    a,b = inm()
    a -= 1
    b -= 1
    dst[a].append(b)
    dst[b].append(a)

for i in range(n):
    if len(dst[i]) >= k:
        print(0)
        exit()

d = len(dst[0])
x = perm(k,d+1)
for nd in dst[0]:
    x = (x * dfs(0,nd)) % R
print(x)

Buggy submission (verdict: Runtime Error):
import sys,heapq,resource
from collections import deque,defaultdict
printn = lambda x: sys.stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True # and False
R = 10**9 + 7
def ddprint(x):
  if DBG:
    print(x)

def perm(k,d):
    x = k
    for i in range(1,d):
        x = (x * (k-i)) % R
    return x

def dfs(prev, cur):
    d = len(dst[cur])-1
    if d==0:
        return 1
    x = perm(k-2,d)
    for nd in dst[cur]:
        if nd==prev:
            continue
        x = (x * dfs(cur,nd)) % R
    return x

sys.setrecursionlimit(250000)
soft,hard = resource.getrlimit(resource.RLIMIT_STACK)
resource.setrlimit(resource.RLIMIT_STACK,(200000,hard))
n,k = inm()
dst = [ [] for i in range(n) ]
for i in range(n-1):
    a,b = inm()
    a -= 1
    b -= 1
    dst[a].append(b)
    dst[b].append(a)

d = len(dst[0])
x = perm(k,d+1)
for nd in dst[0]:
    x = (x * dfs(0,nd)) % R
print(x)