Changed the modInverse function to use the Extended Euclidean Algorithm