I have seen many codes where if we need to output the ans mod, many people do ans = (x + MOD) % MOD (MOD = 1e9+7). But what’s the benefit of doing a + MOD, after all it’s all going to be the same.
It handles the cases where x becomes negative.(there is a chance that x can be negative)
But if x is negative and x < MOD (abs(x) > MOD) then (x+MOD) doesn’t help much.
when you do %mod , the value of x will never become greater than x, hence it will always be useful.
You can simply do this (x%mod + mod)%mod.
You don’t need +mod when you’re doing only additions. Imaging you have big numbers and you gotta add some and subtract some.
say you have 1e9+6 + 3 %mod = 2, now u subtract some other big number say 1e9 + 5, you get negative result
i.e., = - (1e9 + 3) , you do MOD of that, you get same result. so adding MOD to it takes care of this issue.