Try This at Home
시간 제한1초메모리 제한2048 MB
원소 집합을 유지하는 다음 순열을 반복 적용해, 어떤 값이 정확히 한 번 등장하는 최초 단계를 구한다.
문제
For an integer array , let's define as the lexicographically smallest array of the same length that is lexicographically greater than and its set of elements is the same as 's set of elements. If such an array does not exist, is undefined. For example, . In this problem, "the set of array's elements" means an unordered collection of array's elements, where each element is only considered once regardless of how many times it appears in that array. Arrays and have the same set of elements , despite some values appearing a different amount of times in the first and the second array.
Let denote the function applied times to ; here, we consider as undefined too. For example, .
You are given an integer array of length . In this problem, the array satisfies an additional constraint: every value in the array appears there at least twice. Find the smallest positive integer such that is not undefined and the array contains some value that appears exactly once in it, or report that there is no such . As the answer may be very large, find it modulo .
입력
The input contains one or more test cases. The first line contains the number of test cases ().
Each test case is given on two lines. The first of these lines contains an integer (). The second line contains integers (). Every value in the array appears there at least twice.
The sum of for all test cases does not exceed .
출력
For each test case, output a single line with the answer modulo , or -1 if there is no answer.