What about
if (a > 0) return a % n; if (a < 0) { r = n - (-a % n); if (r == n) return 0; return r; }
If a <0, then r = -a % n is the value in [0, n) such that k * n + r = -a for some integer k. Then n - r is the value in (0, n], and since -r = a + k * n, we have n - r = a + (k + 1) * n or = (n - r) + (- k - 1) * n. It follows that n - r is the module a, and since it is located in (0, n], it is non-negative.
Finally, you want the result to be in the range [0, n) and not in (0, n]. To do this, we check if r is equal to n, and if so, return 0. (Which of the course module is n equivalent n)
Joren source share