Playing with Permutations

CodeForces - 251B