I need to find the value of
$$ n(n+1)/2 \pmod m $$
Note: $n \leq 10^{18}$ So intermediate calculations may overflow (I'm using a 64 bit data type to store n)
If m is odd, I can simply find x in $ 2^{-1} \equiv x \pmod m $ and multiply that to $ (n \mod m) * ((n+1) \mod m) $
But what if m is even? The multiplicative inverse won't exist. How do I calculate it then?
n*(n+1) is always an even integer, and if m is even, $ n(n+1) \mod m $ will also be even. So will just dividing by 2 give me the correct answer?