• + 1 comment

    Consider without modulo. a = bx, so to solve for x, x = a/b. Unfortunately we can't really divide under modulo, but we can write this equation as a * b^-1 = x or x is a times the multiplicative inverse of b.

    Luckily we can find the mulitplicative inverse of b under modulo M (because M=10^9 + 7 is prime, so b | b < M is coprime to M). We can calculate it by (b^(M-2))%M