Factor Tree -passing sample testcases but giving run time error in each substask!

my code is running all the sample testcases but unable to run the given subtask please suggest changes in my code.
https://www.codechef.com/viewsolution/31842883