How to calculate permutations