Don't Try This at Home

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

요약
배열 a에서 시작해 서로 다른 원소 집합을 유지하는 다음 순열을 반복 적용하며, 어떤 값의 등장 횟수가 1과 2 사이에서 바뀔 때까지의 최소 반복 횟수를 구한다.
난이도

보통10점 중 5점

유형
배열, 그리디, 정렬, 조합론
정답자
아직 제출이 없습니다

문제

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(\[2,7,7,5,4])=\[4,2,2,5,7]f(\[2,7,7,5,4]) = \[4,2,2,5,7]. 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 \[2,7,7,5,4]\[2,7,7,5,4] and \[4,2,2,5,7]\[4,2,2,5,7] have the same set of elements 2,4,5,7\\{2,4,5,7\\}, 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(\[2,7,7,5,4])=f(\[4,2,2,5,7])=\[4,2,2,7,5]f^2(\[2,7,7,5,4]) = f(\[4,2,2,5,7]) = \[4,2,2,7,5].

You are given an integer array aa of length nn. In this problem, the array satisfies an additional constraint: at least one integer appears exactly once in the array aa. Find the smallest positive integer kk such that fk(a)f^k(a) is not undefined and the array fk(a)f^k(a) satisfies at least one of the following conditions, or report that there is no such kk:

  • there is an integer that appears only once in aa, but at least twice in fk(a)f^k(a);
  • there is an integer that appears at least twice in aa, but only once in fk(a)f^k(a).

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). There is at least one value that appears exactly once in aa.

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

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