MODULO with 10^9+7

Is (a - b) mod m same as ((a mod m) - (b mod m)) mod m?I’m having hard time dealing with it.Also if (a mod m) < (b mod m) then answer should be ((a mod m) - (b mod m) + m) mod m? I tried using library function fmodl but it also giving negative answers.Also if I use unsigned numbers then output is something different.

So what is the correct way to calculate (a-b) mod m where a>=b>=m in C++.

Assume the expression as ((a)mod m+(-b)mod m)mod m [expression (1)]

Now,define mod by yourself as

int mod(int a,int m)


return (a%m+m)%m;


The expression(1) can be represented in c++ as:

int ans=mod(mod(a,m)+(mod(-b,m),m);

Reference for my answer: [click here][1]

I gave the c++ implementation of the concept on the above link.

I hope this helps.