WA for tree product

I am getting wrong answer for my solution. I am using simple recursion and pretty sure that my implementation is correct. Can anyone help me in finding error in my


  [1]: http://www.codechef.com/viewsolution/1102840

You can’t do mod operation in recursion. The most correct way is to use your own BigInteger class for calculations. And only in the end take mod. But test data for this problem are rather weak. So alternatively you can made all calculations in double with saving for each vertex from what son it receives its maximum and then in the end restore the answer modulo 10^9+7 using this information. Another way is to store answer or each vertex in double as well as modulo 10^9+7 and when decide from which son you should get value use double and for the answer use stored answer by modulo.


Thank you very much anton_lunyov. After correcting my code for mod operation and using long double for saving maximum, I got it accepted!!!