How can I solve for F in the following situation?
$$ F \equiv \frac{n}{d} \pmod{m} \tag 1$$
Denominator d always divides n, but I cannot reduce the fraction directly because n is always huge, and I would need to do modular operations on it first. Denominator d and m are often relatively prime. In those cases I can get to the answer by computing the modular inverse of d modulo m.
Often however denominator d and m are not relatively prime. Let's say $g = gcd(d, m) > 1$. I rewrite the congruence and solve for F:
$$ d \ F \equiv n \pmod{m} \tag 2$$
I can find all g distinct solutions for F in this equation. But how can I determine which is the one that is the answer to the initial equation?