Use Base.isperm() to check validity of the permutation.
This is more efficient than the previous method.