faster method of computing modular inverse (thanks @danaj)