unroll loops manually in modular_inverse (suggested by @danaj)