Implement applyPermutation.