Runtime error in Permutation Swaps ques of may cook off

Can somebody tell why am I getting runtime error in the question.
Problem Link:-

My Solution:-

Hint :- The problem that I am getting rotates around cnt variable I have defined. I need to do cnt++ which causes the runtime error, but if I replace it with just cnt it shows wrong answer.