Matrix Exponentiation Implementation

I’m getting TLE in this question. This is my code:-