Adding validation in modPow method