How to take MMI?

Please someone explain to me that in the probabilities problem, they ask to give ans as P*Q^-1 mod M. I am assuming we have to take the inverse of Q and then multiply it by P by taking the mod of that and we will have our final answer. Please correct me if i am wrong. Also, I am not understanding the inverse function in this code.

For a given integer ‘Q’ and prime ‘M’ it is possible to find ‘R’ such that Q*R = 1 (mod M). R is said to be the inverse of Q (mod M). That is, this problem is referring to the modular inverse, not the rational number 1/Q.

Once you have solved the problem to find P and Q, you will need to find ‘R’ as described above with M = 1000000007. Then (P*R) % 1000000007 will be your answer.

Hi there, See

Modular Multiplicative Inverse

Although the code is written in Rust, you will find it very helpful for knowing the concept.