Don't Try This at Home
시간 제한1초메모리 제한2048 MB
배열 a에서 시작해 서로 다른 원소 집합을 유지하는 다음 순열을 반복 적용하며, 어떤 값의 등장 횟수가 1과 2 사이에서 바뀔 때까지의 최소 반복 횟수를 구한다.
문제
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: at least one integer appears exactly once in the array . Find the smallest positive integer such that is not undefined and the array satisfies at least one of the following conditions, or report that there is no such :
- there is an integer that appears only once in , but at least twice in ;
- there is an integer that appears at least twice in , but only once in .
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 (). There is at least one value that appears exactly once in .
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.