Implemented powmod (exponentiation by squaring algorithm)