Help me in solving MATMOD problem

This is my solution for MATMOD (complete explanation in the comments of the code)
I have checked it with all the edge cases I could think of , by taking accepted code and comparing the output
Still I am getting wrong answer
My submission : https://www.codechef.com/viewsolution/1046692247