#P1658C. Shinju and the Lost Permutation

    ID: 1286 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>constructive algorithmsmath*1700

Shinju and the Lost Permutation

Description

Shinju loves permutations very much! Today, she has borrowed a permutation pp from Juju to play with.

The ii-th cyclic shift of a permutation pp is a transformation on the permutation such that p=[p1,p2,,pn]p = [p_1, p_2, \ldots, p_n] will now become p=[pni+1,,pn,p1,p2,,pni] p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}].

Let's define the power of permutation pp as the number of distinct elements in the prefix maximums array bb of the permutation. The prefix maximums array bb is the array of length nn such that bi=max(p1,p2,,pi)b_i = \max(p_1, p_2, \ldots, p_i). For example, the power of [1,2,5,4,6,3][1, 2, 5, 4, 6, 3] is 44 since b=[1,2,5,5,6,6]b=[1,2,5,5,6,6] and there are 44 distinct elements in bb.

Unfortunately, Shinju has lost the permutation pp! The only information she remembers is an array cc, where cic_i is the power of the (i1)(i-1)-th cyclic shift of the permutation pp. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.

Given the array cc, determine if there exists a permutation pp that is consistent with cc. You do not have to construct the permutation pp.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3, 4] is also not a permutation (n=3n=3 but there is 44 in the array).

The input consists of multiple test cases. The first line contains a single integer tt (1t51031 \leq t \leq 5 \cdot 10^3) — the number of test cases.

The first line of each test case contains an integer nn (1n1051 \le n \le 10^5).

The second line of each test case contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n (1cin1 \leq c_i \leq n).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Input

The input consists of multiple test cases. The first line contains a single integer tt (1t51031 \leq t \leq 5 \cdot 10^3) — the number of test cases.

The first line of each test case contains an integer nn (1n1051 \le n \le 10^5).

The second line of each test case contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n (1cin1 \leq c_i \leq n).

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

Output

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Sample Input 1

6
1
1
2
1 2
2
2 2
6
1 2 4 6 3 5
6
2 3 1 2 3 4
3
3 2 1

Sample Output 1

YES
YES
NO
NO
YES
NO

Note

In the first test case, the permutation [1][1] satisfies the array cc.

In the second test case, the permutation [2,1][2,1] satisfies the array cc.

In the fifth test case, the permutation [5,1,2,4,6,3][5, 1, 2, 4, 6, 3] satisfies the array cc. Let's see why this is true.

  • The zeroth cyclic shift of pp is [5,1,2,4,6,3][5, 1, 2, 4, 6, 3]. Its power is 22 since b=[5,5,5,5,6,6]b = [5, 5, 5, 5, 6, 6] and there are 22 distinct elements — 55 and 66.
  • The first cyclic shift of pp is [3,5,1,2,4,6][3, 5, 1, 2, 4, 6]. Its power is 33 since b=[3,5,5,5,5,6]b=[3,5,5,5,5,6].
  • The second cyclic shift of pp is [6,3,5,1,2,4][6, 3, 5, 1, 2, 4]. Its power is 11 since b=[6,6,6,6,6,6]b=[6,6,6,6,6,6].
  • The third cyclic shift of pp is [4,6,3,5,1,2][4, 6, 3, 5, 1, 2]. Its power is 22 since b=[4,6,6,6,6,6]b=[4,6,6,6,6,6].
  • The fourth cyclic shift of pp is [2,4,6,3,5,1][2, 4, 6, 3, 5, 1]. Its power is 33 since b=[2,4,6,6,6,6]b = [2, 4, 6, 6, 6, 6].
  • The fifth cyclic shift of pp is [1,2,4,6,3,5][1, 2, 4, 6, 3, 5]. Its power is 44 since b=[1,2,4,6,6,6]b = [1, 2, 4, 6, 6, 6].

Therefore, c=[2,3,1,2,3,4]c = [2, 3, 1, 2, 3, 4].

In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array cc.