Problem p03175 (python)

Fixed submission:
######################################################
############Created by Devesh Kumar###################
#############devesh1102@gmail.com####################
##########For CodeForces(Devesh1102)#################
#####################2020#############################
######################################################
import sys
input = sys.stdin.readline

# import sys
import heapq 
import copy
import math
import decimal
# import sys.stdout.flush as flush
# from decimal import *
#heapq.heapify(li) 
#
#heapq.heappush(li,4) 
#
#heapq.heappop(li)
#
# &	Bitwise AND Operator	10 & 7 = 2
# |	Bitwise OR Operator	10 | 7 = 15
# ^	Bitwise XOR Operator	10 ^ 7 = 13

# <<	Bitwise Left Shift operator	10<<2 = 40
# >>	Bitwise Right Shift Operator
# '''############ ---- Input Functions ---- #######Start#####'''


def inp():
	return(int(input()))
def inlt(): 
	return(list(map(int,input().split())))
def insr():
	s = input()
	return(list(s[:len(s) - 1]))
def insr2():
	s = input()
	return((s[:len(s) - 1]))
def invr():
	return(map(int,input().split()))
############ ---- Input Functions ---- #######End
# #####   

def pr_list(a):
	print(*a, sep=" ")
def main():
	# tests =  inp()
	tests = 1
	mod = 1000000007
	limit = 10**18
	ans = 0
	start = {}
	def pre_sum(arr):
		ans = [arr[0]]
		for i in range(1,len(arr)):
			ans.append(ans[-1]+ arr[i])
		return ans
	for test in range(tests):
		n = inp()
		branch = {}

		# parent = [0 for i in range(n+1)]
		for i in range(n-1):
			[p,r] =  inlt()
			if p in branch:
				branch[p].append(r)
			else:
				branch[p] = [r]
			if r in branch:
				branch[r].append(p)
			else:
				branch[r] = [p]
		parent = [-1 for i in range(n+1)]
		# print(branch)
		# print(parent)
		# print(branch)

		dp = [[0 for j in range(n+1)] for i in range(2)]
		leaf = {}
		ans = 2
		stack = [1]
		parent[1] = 0
		if n == 1:
			print(2)
			continue
		while(stack!=[]):
			# print(stack)
			curr = stack[-1]
			flag = 0
			for i in branch[curr]:
				if parent[i] == -1:
					parent[i] = curr
					stack.append(i)
					flag = 1
			if flag == 0:
				curr= stack.pop()
				white = 1
				black = 1
				for i in branch[curr]:
					if i !=parent[curr]:
						white = (white*(dp[0][i] + dp[1][i]))%mod
						black = (black*(dp[0][i]))%mod
				dp[0][curr] = white
				dp[1][curr] = black

		print((dp[0][1] + dp[1][1])%mod) 


		# for i in range(1,n+1):
		# 	if i not in branch:
		# 		leaf[i] = 1
		# 		dp[0][i] =1
		# 		dp[1][i] =1
		# # print(leaf)
		# while(leaf!={}):
		# 	nexts = {}
		# 	for i in leaf:
		# 		nexts[parent[i]] = 1
		# 	# print(nexts)
		# 	if 0 in nexts:
		# 		del nexts[0]
		# 	for i in nexts:
		# 		white = 1
		# 		black = 1
				
		# 		for j in branch[i]:
		# 			white = white*(dp[0][j] + dp[1][j])
		# 			black = black * dp[1][j]##1 is white
		# 		dp[0][i] = black
		# 		dp[1][i] = white
		# 		ans= max(ans, dp[0][i]+dp[1][i])
		# 	leaf = nexts
		# print(ans)




		


if __name__== "__main__":
	main()

Buggy submission (verdict: Runtime Error):
	######################################################
	############Created by Devesh Kumar###################
	#############devesh1102@gmail.com####################
	##########For CodeForces(Devesh1102)#################
	#####################2020#############################
	######################################################
	import sys
	input = sys.stdin.readline

	# import sys
	import heapq 
	import copy
	import math
	import decimal
	# import sys.stdout.flush as flush
	# from decimal import *
	#heapq.heapify(li) 
	#
	#heapq.heappush(li,4) 
	#
	#heapq.heappop(li)
	#
	# &	Bitwise AND Operator	10 & 7 = 2
	# |	Bitwise OR Operator	10 | 7 = 15
	# ^	Bitwise XOR Operator	10 ^ 7 = 13
	
	# <<	Bitwise Left Shift operator	10<<2 = 40
	# >>	Bitwise Right Shift Operator
	# '''############ ---- Input Functions ---- #######Start#####'''
	

	def inp():
		return(int(input()))
	def inlt(): 
		return(list(map(int,input().split())))
	def insr():
		s = input()
		return(list(s[:len(s) - 1]))
	def insr2():
		s = input()
		return((s[:len(s) - 1]))
	def invr():
		return(map(int,input().split()))
	############ ---- Input Functions ---- #######End
	# #####   

	def pr_list(a):
		print(*a, sep=" ")
	def main():
		# tests =  inp()
		tests = 1
		mod = 1000000007
		limit = 10**18
		ans = 0
		start = {}
		def pre_sum(arr):
			ans = [arr[0]]
			for i in range(1,len(arr)):
				ans.append(ans[-1]+ arr[i])
			return ans
		for test in range(tests):
			n = inp()
			branch = {}

			# parent = [0 for i in range(n+1)]
			for i in range(n-1):
				[p,r] =  inlt()
				if p in branch:
					branch[p].append(r)
				else:
					branch[p] = [r]
				if r in branch:
					branch[r].append(p)
				else:
					branch[r] = [p]
			parent = [-1 for i in range(n+1)]
			# print(branch)
			# print(parent)
			# print(branch)

			dp = [[0 for j in range(n+1)] for i in range(2)]
			leaf = {}
			ans = 2
			stack = [1]
			parent[1] = 0
			if n == 1:
				print(2)
				continue
			while(stack!=[]):
				# print(stack)
				curr = stack[-1]
				flag = 0
				for i in branch[curr]:
					if parent[i] == -1:
						parent[i] = curr
						stack.append(i)
						flag = 1
				if flag == 0:
					curr= stack.pop()
					white = 1
					black = 1
					for i in branch[curr]:
						if i !=parent[curr]:
							white = white*(dp[0][i] + dp[1][i])
							black = black*(dp[0][i])
					dp[0][curr] = white
					dp[1][curr] = black

			print(dp[0][1] + dp[1][1]) 


			# for i in range(1,n+1):
			# 	if i not in branch:
			# 		leaf[i] = 1
			# 		dp[0][i] =1
			# 		dp[1][i] =1
			# # print(leaf)
			# while(leaf!={}):
			# 	nexts = {}
			# 	for i in leaf:
			# 		nexts[parent[i]] = 1
			# 	# print(nexts)
			# 	if 0 in nexts:
			# 		del nexts[0]
			# 	for i in nexts:
			# 		white = 1
			# 		black = 1
					
			# 		for j in branch[i]:
			# 			white = white*(dp[0][j] + dp[1][j])
			# 			black = black * dp[1][j]##1 is white
			# 		dp[0][i] = black
			# 		dp[1][i] = white
			# 		ans= max(ans, dp[0][i]+dp[1][i])
			# 	leaf = nexts
			# print(ans)




			


	if __name__== "__main__":
		main()