I was trying to solve Magical Five problem on codeforces. I have correctly formed an equation which I need to solve via program such that resulting number don't overflow. Answer can be Python or C++ specific.
Now, the equation is:
$$\frac{2^{mn-1}}{2^n-1} \bmod{p} \qquad \text{where $p=10^9+7$ is prime}$$
I know denominator divides numerator evenly but I am not able to prove this from this equation.
PS: I know my equation is correct as I have matched my solution against the provided editorial at Solution Page
PPS: I have also read about Fermat's little theorem, which states that $a^p \equiv a \bmod{p}$ where $p$ is prime and $a < p$.
Since for the given range, $2^N - 1$ cannot be a multiple of $p = 10^9 + 7$, you can obtain the result by modular multiplication with the inverse of $2^N - 1$ modulo $p$.
Compute $r = 2^N \pmod{p}$, via exponentiation by repeated squaring, for example (simple to implement, and quite fast). Compute $s = r^M \pmod{p}$, with the same algorithm. Find the modular inverse of $r-1$, for example with the extended Euclidean algorithm. Compute $(s-1)\cdot(r-1)^{-1} \pmod{p}$.
All computations can be done without overflow using a 64-bit integer type.