Problem in dealing with problems which are based on exponentiation and modulus.

I recently tried to solve THIS PROBLEM based on exponentiation and modulus but I couldn’t solve it. Here is the link to MY SOLUTION. I am getting run time error(NZEC) with this solution and when I comment the line-1 and line-2 and remove comment from line-3 I still get NZEC. Please tell me why I am getting NZEC in both the cases and also the right way to solve this question.

PLEASE REFER MY CODE, LINE-1,LINE-2,LINE-3 ARE MENTIONED AS COMMENT OVER THERE