Works fine in C++ but gives RTE in PYTH3

My issue

Giving RTE at
possibility=dfs(child,node,graph,tower,m)
[Previous line repeated 995 more times]
File “/mnt/sol.py”, line 12, in dfs
if child!=parent:
RecursionError: maximum recursion depth exceeded in comparison

My code

# cook your dish here
def ncr(n,r):
    s=1
    for i in range(1, r+1):
        s=s*(n - r + i) / i
    return int(s)
    
def dfs(node,parent,graph,tower,m):
    length=tower[node]
    ways=1
    for child in graph[node]:
        if child!=parent:
            possibility=dfs(child,node,graph,tower,m)
            ways=(((ways*ncr(length+possibility[0],length)%m)*possibility[1])%m)
            length+=possibility[0]
    if length>0:
        length+=1
    return (length,ways)
        
for _ in range(int(input())):
    nodes=int(input())
    towers=list(map(int,input().split()))
    adj=[[] for _ in range(nodes)]
    for _ in range(nodes-1):
        x,y=map(int,input().split())
        adj[x-1].append(y-1)
        adj[y-1].append(x-1)
    print(dfs(0,-1,adj,towers,10**9+7)[1])
    
    

Problem Link: BUILDT Problem - CodeChef

This is because there is limit to number of recursions a function can do in Python. You can increase the recursion depth using

import sys
sys.setrecursionlimit(1500)

You can set the 1500 to a higher number if this is still not sufficient. But be careful as more recursion means more stack memory. The maximum memory your program can use in our IDE is 1.5 GB.