As you noted m needs to be relatively prime to bcd, so if b,c, or d have a factor of m this will not work. Also note that if m is not prime, but is relatively prime to b,c and d then we replace m-2 with phi(m) - 1, where phi is the euler totient function, to find the inverse. i.e for m relatively prime to x we have that x^{-1} (mod m) = x^{phi(m) - 1}

In general it is somewhat slow to compute a^(phi(m) - 1) for large m, so you will want to look up euclid’s extended algorithm to find the modular inverse.

I don’t think this answers you question though. If I am correct then you are asking is equivalent to whether x / y (mod m) = (x / (y % m)) % m? I think this still works as long as y, m are relatively prime.

i read and found that fermat’s little theorem is not applicable if number is divisible by mod. Even though the mod is prime, this will not be applicable when number is equal to prime mod. What can we do in that case?