How to calculate number of permutations