Please anyone tell why ans=(ans+3LL*mod)%mod taken

https://www.codechef.com/viewsolution/93936502

After the calculation on line 560, ‘ans’ is congruent to the final answer mod 10^9 + 7, but may be negative. Line 561 adjusts ‘ans’ so it is on the interval [0, 10^9+7).

Thank you very much mate. But i still little bit confuse i would be grateful if you give one example.

lets after the caluclation on line 560 , ans = - 3 * (10^9 ) so to avoid negative ans or adjusts ‘ans’ so it is on the interval [0, 10^9+7) so he take ans = (ans+3LL*mod)%mod.

we can also do like this :- take first modulo like (ans %= mod) to adjusts ‘ans’ so it is on the interval [0, 10^9+7) . then to avoid negative ans we take ans = (ans + mod)% mod;

you can replace that line by :- ans %= mod;
ans = (ans + mod)% mod;

6 Likes

thank you so much abhi_kant