Try This at Home

시간 제한1초메모리 제한2048 MB

요약
원소 집합을 유지하는 다음 순열을 반복 적용해, 어떤 값이 정확히 한 번 등장하는 최초 단계를 구한다.
난이도

어려움10점 중 8점

유형
조합론, 수학, 그리디, 구현
정답자
아직 제출이 없습니다

문제

For an integer array bb, let's define f(b)f(b) as the lexicographically smallest array of the same length that is lexicographically greater than bb and its set of elements is the same as bb's set of elements. If such an array does not exist, f(b)f(b) is undefined. For example, f(\[3,2,3,2,6,6])=\[3,2,3,3,2,6]f(\[3,2,3,2,6,6]) = \[3,2,3,3,2,6]. 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 \[3,2,3,2,6,6]\[3,2,3,2,6,6] and \[3,2,3,3,2,6]\[3,2,3,3,2,6] have the same set of elements 2,3,6\\{2,3,6\\}, despite some values appearing a different amount of times in the first and the second array.

Let fk(b)f^k(b) denote the function ff applied kk times to bb; here, we consider f(undefined)f(\text{undefined}) as undefined too. For example, f2(\[3,2,3,2,6,6])=f(\[3,2,3,3,2,6])=\[3,2,3,3,3,6]f^2(\[3,2,3,2,6,6]) = f(\[3,2,3,3,2,6]) = \[3,2,3,3,3,6].

You are given an integer array aa of length nn. In this problem, the array satisfies an additional constraint: every value in the array aa appears there at least twice. Find the smallest positive integer kk such that fk(a)f^k(a) is not undefined and the array fk(a)f^k(a) contains some value that appears exactly once in it, or report that there is no such kk. As the answer may be very large, find it modulo 109+710^9 + 7.

입력

The input contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5).

Each test case is given on two lines. The first of these lines contains an integer nn (2≤n≤1052 \le n \le 10^5). The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n (1≤a_i≤1061 \le a\_i \le 10^6). Every value in the array aa appears there at least twice.

The sum of nn for all test cases does not exceed 6⋅1056 \cdot 10^5.

출력

For each test case, output a single line with the answer modulo 109+710^9 + 7, or -1 if there is no answer.

예제1

  1. 예제 1

    입력
    3
    6
    3 2 3 2 6 6
    3
    6 6 6
    8
    1 1 4 3 3 4 1 1
    
    예상 출력
    1
    -1
    9