Problem p02985 (python)

Fixed submission:
from collections import deque
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
q=deque()
q.append(0)
e=b
while len(q):
  i=q.pop()
  h=len(d[i])
  g[i]=1
  if i==0:
      f=b-1
  else:
    f=b-2
  g[i]=1
  for j in range(h):
    if g[d[i][j]]==0:
      q.append(d[i][j])
      e=(e*f)%1000000007
      f-=1


print(e%1000000007)

Buggy submission (verdict: Runtime Error):
import math
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
def s(i,d,g):
  g[i]=1
  global e
  global bb
  f=1
  h=len(d[i])
  if i==0:
    for j in range(h):
      f=f*(b-1-j)
  else:
    if h>0:
      for j in range(h-1):
        f=f*(b-2-j)
  e=(e*f)%1000000007
  for j in range(h):
    if g[d[i][j]]==0:
      s(d[i][j],d,g)
  return

e=b
s(0,d,g)
print(e%1000000007)

Buggy submission (verdict: Runtime Error):
import math
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
def s(i,d,g):
  g[i]=1
  global e
  global bb
  f=1
  h=len(d[i])
  if i==0:
    for j in range(h):
      f=f*(b-1-j)
  else:
    if h>0:
      for j in range(h-1):
        f=f*(b-2-j)
  e=(e*f)%1000000007
  for j in range(h):
    if g[d[i][j]]==0:
      s(d[i][j],d,g)
  return

e=b
s(0,d,g)
print(e%1000000007)

Buggy submission (verdict: Runtime Error):
import math
a,b=map(int,input().split())
bb=math.factorial(b-2)
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
def s(i,d,g):
  g[i]=1
  global e
  global bb
  if i==0:
    f=bb*(b-1)//math.factorial(b-len(d[i])-1)
  else:
    if len(d[i])==0:
      f=1
    else:
      f=bb//math.factorial(b-len(d[i])-1)   
  e=(e*f)%1000000007
  for j in range(len(d[i])):
    if g[d[i][j]]==0:
      s(d[i][j],d,g)
  return

i=0
e=b
s(i,d,g)
print(e%1000000007)

Buggy submission (verdict: Runtime Error):
import math
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
def s(i,d,g):
  g[i]=1
  global e
  if i==0:
    f=math.factorial(b-1)//math.factorial(b-len(d[i])-1)
  else:
    f=math.factorial(b-2)//math.factorial(b-len(d[i])-1)   
  e=e*f
  for j in range(len(d[i])):
    if g[d[i][j]]==0:
      s(d[i][j],d,g)
  return

i=0
e=b
s(i,d,g)
print(e%1000000007)

Buggy submission (verdict: Runtime Error):
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a-1)]
d=[[]for i in range(a)]
for i in range(a-1):
  ca=c[i][0]
  cb=c[i][1]
  d[ca-1].append(cb-1)
  d[cb-1].append(ca-1)
g=[0 for i in range(a)]
def s(i,d,g):
  g[i]=1
  global e
  f=1
  if i==0:
    for j in range(len(d[i])):
      f=f*(b-1-j)
  else:
    for j in range(len(d[i])-1):
      f=f*(b-2-j)   
  e=e*f
  for j in range(len(d[i])):
    if g[d[i][j]]==0:
      s(d[i][j],d,g)
  return

i=0
e=b
s(i,d,g)
print(e%1000000007)