Modulo inverse calculation verification

I want to calculate this :

( (a*b) / g ) %MOD

where MOD is 10^9+7

Is the following way correct way to do it in code?

long long a, b, g; //assume value is already there. And a,b,g are not >mod long long res = a*b % mod; long long int inverse = inv(g); maxlen = res/inverse;

Where inv function is as below:

#define ull unsigned long long
long long int inv(long long int a) {
return pow_(a,mod-2,mod);
}

And pow function is

#define mod 1000000007 long long int pow_(long long int base,long long int exp,long long int m) { long long int ret=1; base%=m; while(exp) { if(exp&1) { ret*=base; ret%=m; } base*=base; base%=m; exp>>=1; } return ret%m; }

If the functions are wrong, Could anyone please provide me with a function that provides correct result?

2 Likes

What is maxlen ? if it is our final result ( (ab)/g )%MOD than it should be maxlen = resinverse ;

maxlen is where i store the result of the entire calculation. Ah! i see where i was wrong.