Counting distinct permutations with specific conditions