modInverse and modPower now use hand written code