Add these lines to speed up your program for 2nd test case:
If you have done Corey's Party (AIO 2014: Australia), (much harder)which appears earlier on this playlist, you should have no problem in this question.
If you don't know about sets, this question can also be done via BFS/DFS as explained in other hints.
Thanks for the solution.
I understand why you are using modular exponentiation. But, I have a small doubt: We have to calculate (2^N - repeats)%m. But modular exponentiation can only calculate (2^N)%m.
Btw, the solution link you provided is broken. Can you please share the solution again ?