binary algorithm for modular inversion