rewrite next permutation