note that n is required to be a power of 2 for the divide-and-conquer algorithm to work. this means that gcd(n,2p^k) = 2, so n is not invertible modulo 2p^k. this prevents us from using this modulus despite the fact that the multiplicative group is cyclic.
we add an assertion in the modular inverse function to ensure that the input is actually invertible which was missing before.