Problem p03175 (python)

Fixed submission:
from sys import setrecursionlimit
setrecursionlimit(10**9)

N = int(input())
MOD = 10**9 + 7

tree = [[] for i in range(N)]
for i in range(N-1):
  x, y = map(int, input().split())
  tree[x-1].append(y-1)
  tree[y-1].append(x-1)

dp1 = [0]*N # 頂点iが白のときに、頂点iを根とする部分木の塗り方の組み合わせ
dp2 = [0]*N # 頂点iが黒のときに、頂点iを根とする部分木の塗り方の組み合わせ


# nodeを白で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve1(node, parent): 
  global dp1
  global dp2
  if dp1[node]:
    return dp1[node]
  else:
    children = tree[node]
    if len(children)==0 or \
        len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp1[node] = 1 # nodeを白に塗る1通りだけ
      return dp1[node]
    
    else:
      dp1[node] = 1
      for child in children:
        if child == parent:
          continue

        dp1[node] *= solve1(child, node) + solve2(child, node)
        dp1[node] %= MOD

      return dp1[node]


# nodeを黒で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve2(node, parent):
  global dp1
  global dp2
  if dp2[node]:
    return dp2[node]
  else:
    children = tree[node]
    if len(children)==0 or \
        len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp2[node] = 1 # nodeを黒に塗る1通りだけ
      return dp2[node]
    
    else:
      dp2[node] = 1
      for child in children:
        if child == parent:
          continue

        dp2[node] *= solve1(child, node)
        dp2[node] %= MOD

      return dp2[node]


print((solve1(0, -1)+solve2(0, -1))%MOD)

Buggy submission (verdict: Runtime Error):
N = int(input())
MOD = 10**9 + 7

tree = [[] for i in range(N)]
for i in range(N-1):
  x, y = map(int, input().split())
  tree[x-1].append(y-1)
  tree[y-1].append(x-1)

dp1 = [0]*N # 頂点iが白のときに、頂点iを根とする部分木の塗り方の組み合わせ
dp2 = [0]*N # 頂点iが黒のときに、頂点iを根とする部分木の塗り方の組み合わせ


# nodeを白で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve1(node, parent): 
  global dp1
  global dp2
  if dp1[node]:
    return dp1[node]
  else:
    children = tree[node]
    if len(children)==0 or \
        len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp1[node] = 1 # nodeを白に塗る1通りだけ
      return dp1[node]
    
    else:
      dp1[node] = 1
      for child in children:
        if child == parent:
          continue

        dp1[node] *= solve1(child, node) + solve2(child, node)
        dp1[node] %= MOD

      return dp1[node]


# nodeを黒で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve2(node, parent):
  global dp1
  global dp2
  if dp2[node]:
    return dp2[node]
  else:
    children = tree[node]
    if len(children)==0 or \
    len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp2[node] = 1 # nodeを黒に塗る1通りだけ
      return dp2[node]
    
    else:
      dp2[node] = 1
      for child in children:
        if child == parent:
          continue

        dp2[node] *= solve1(child, node)
        dp2[node] %= MOD

      return dp2[node]


print((solve1(0, -1)+solve2(0, -1))%MOD)

Buggy submission (verdict: Runtime Error):
N = int(input())
MOD = 10**9 + 7

tree = [[] for i in range(N)]
for i in range(N-1):
  x, y = map(int, input().split())
  tree[x-1].append(y-1)
  tree[y-1].append(x-1)

dp1 = [0]*N # 頂点iが白のときに、頂点iを根とする部分木の塗り方の組み合わせ
dp2 = [0]*N # 頂点iが黒のときに、頂点iを根とする部分木の塗り方の組み合わせ


# nodeを白で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve1(node, parent): 
  global dp1
  global dp2
  if dp1[node]:
    return dp1[node]
  else:
    children = tree[node]
    if len(children)==0 or \
        len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp1[node] = 1 # nodeを白に塗る1通りだけ
      return dp1[node]
    
    else:
      dp1[node] = 1
      for child in children:
        if child == parent:
          continue

        dp1[node] *= solve1(child, node) + solve2(child, node)
        dp1[node] %= MOD

      return dp1[node]


# nodeを黒で塗るときに、nodeを根とする部分木の塗り方の組み合わせを求める
def solve2(node, parent):
  global dp1
  global dp2
  if dp2[node]:
    return dp2[node]
  else:
    children = tree[node]
    if len(children)==0 or \
    len(children)==1 and children[0]==parent: # nodeがリーフのとき
      dp2[node] = 1 # nodeを黒に塗る1通りだけ
      return dp2[node]
    
    else:
      dp2[node] = 1
      for child in children:
        if child == parent:
          continue

        dp2[node] *= solve1(child, node)
        dp2[node] %= MOD

      return dp2[node]


print(solve1(0, -1) + solve2(0, -1))