Road Improvement - Codeforces

Can somebody please help me in this question?
I have calculated ans for one node by randomly selecting it and taking that node as root by applying dfs from that randomly selected node using this function
f(node) =
product_for_all_children(1+ f(child_i)). And filled the dp array using these.

This gives answer for the root node and calculates answer for all nodes without multiplying answer of parent.
Then to add the answer from parent I tried to do this by applying bfs from that randomly selected root using this equation
f(child) *= 1 + f(parent)/(1+f(child)).
This works fine till all values are less than 1e9 + 7 but beyond that it gives WA because in the dp array I’m not storing the exact value of f(node) but f(node)%MOD which creates problem for the second equation.
What should I do to get the correct value of f(node) through second equation even after taking MOD?
Here’s my code