Help needed in codeforces problem div2 D

https://codeforces.com/contest/1401/problem/D
I am trying to solve this problem but i am getting wa in test no 5 please someone can help me through this… can someone please spot out where i am making mistake …
i have provided proper explanation in my code …link is below

Thanks in Advane

the problem is you are sorting (because of set)after doing mod of the factors and also the no of nodes

1 Like

Here is the modified accepted solution of your code and here is the diff between those two.

Explanation

1 Like

thanks to both of you i realised my fault … and i hope i will not repeat that stupid mistake …
Thanks for giving your precious time and saving my day…

3 Likes

first of all take all things in long long
compute carefully modular multiplication , and modular add .

more refer_my_solution

1 Like

Thanks @ssrivastava990 for help i realised my mistake