Return all unique permutations of a list that may contain duplicates, in lexicographic order, without generating duplicates and filtering them afterwards.
Also implement next_permutation(nums): rearrange nums in place into the next lexicographically greater permutation, or the smallest (sorted) one if it's already the largest. Return nothing.