After reading the editorial, I’m trying to solve this problem. But I don’t know why I’m keep getting WA.

Here is my solution. I’m getting WA in **sub-task 3 & 4**. I can’t find why? Please, help me out.

you have added -1 in the sum and suppose everything else is 0 but -1 remains then -1 mod m is -1 which is WA hence just add while(sum<0) sum+=m; i guess then it should work correctly.

tried…didn’t work…

Your problem is on line 97. (the if statement)

It is true that you don’t need to add the factorial part if x > m but you have to include the remaining part of the formula.

ohh…yes…silly mistake . thanks a lot