Subtraction works similarly: (a - b) mod m = ((a mod m) - (b mod m)) mod m. But subtraction can give negative results.
Example: (7 - 10) mod 5. The difference is -3. In C++, -3 % 5 gives -3, not 2. You need to add m: (-3 + 5) % 5 = 2.
Safe formula: ((a - b) % m + m) % m. This always gives a result between 0 and m - 1.