Shinju loves permutations very much! Today, she has borrowed a permutation $$$p$$$ from Juju to play with.The $$$i$$$-th cyclic shift of a permutation $$$p$$$ is a transformation on the permutation such that $$$p = [p_1, p_2, \ldots, p_n] $$$ will now become $$$ p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}]$$$.Let's define the power of permutation $$$p$$$ as the number of distinct elements in the prefix maximums array $$$b$$$ of the permutation. The prefix maximums array $$$b$$$ is the array of length $$$n$$$ such that $$$b_i = \max(p_1, p_2, \ldots, p_i)$$$. For example, the power of $$$[1, 2, 5, 4, 6, 3]$$$ is $$$4$$$ since $$$b=[1,2,5,5,6,6]$$$ and there are $$$4$$$ distinct elements in $$$b$$$.Unfortunately, Shinju has lost the permutation $$$p$$$! The only information she remembers is an array $$$c$$$, where $$$c_i$$$ is the power of the $$$(i-1)$$$-th cyclic shift of the permutation $$$p$$$. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.Given the array $$$c$$$, determine if there exists a permutation $$$p$$$ that is consistent with $$$c$$$. You do not have to construct the permutation $$$p$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^3$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$1 \leq c_i \leq n$$$).  It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print "YES" if there is a permutation $$$p$$$ exists that satisfies the array $$$c$$$, 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).

NoteIn the first test case, the permutation $$$[1]$$$ satisfies the array $$$c$$$.In the second test case, the permutation $$$[2,1]$$$ satisfies the array $$$c$$$.In the fifth test case, the permutation $$$[5, 1, 2, 4, 6, 3]$$$ satisfies the array $$$c$$$. Let's see why this is true.  The zeroth cyclic shift of $$$p$$$ is $$$[5, 1, 2, 4, 6, 3]$$$. Its power is $$$2$$$ since $$$b = [5, 5, 5, 5, 6, 6]$$$ and there are $$$2$$$ distinct elements — $$$5$$$ and $$$6$$$.  The first cyclic shift of $$$p$$$ is $$$[3, 5, 1, 2, 4, 6]$$$. Its power is $$$3$$$ since $$$b=[3,5,5,5,5,6]$$$.  The second cyclic shift of $$$p$$$ is $$$[6, 3, 5, 1, 2, 4]$$$. Its power is $$$1$$$ since $$$b=[6,6,6,6,6,6]$$$.  The third cyclic shift of $$$p$$$ is $$$[4, 6, 3, 5, 1, 2]$$$. Its power is $$$2$$$ since $$$b=[4,6,6,6,6,6]$$$.  The fourth cyclic shift of $$$p$$$ is $$$[2, 4, 6, 3, 5, 1]$$$. Its power is $$$3$$$ since $$$b = [2, 4, 6, 6, 6, 6]$$$.  The fifth cyclic shift of $$$p$$$ is $$$[1, 2, 4, 6, 3, 5]$$$. Its power is $$$4$$$ since $$$b = [1, 2, 4, 6, 6, 6]$$$. Therefore, $$$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 $$$c$$$.

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Fprintln(out, "NO")
			continue
		}
		last := arr[pos]
		for i := pos; i < n; i++ {
			if arr[i] - last > 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
			last = arr[i]
		}
		last = arr[n-1]
		for i := 0; i < pos; i++ {
			if arr[i] - last > 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
			last = arr[i]
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
NO
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneCnt++
		}
	}
	scanf("\n")

	if oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	c = append(c, c[0])

	for i := 0; i < len(c)-1; i++ {
		lft := c[i]
		rht := c[i+1]
		if rht-lft > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		var z int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				z++
			}
		}

		arr = append(arr, arr[0])
		f := true
		for i := 0; i < n; i++ {
			if arr[i+1]-arr[i] > 1 {
				f = false
				break
			}
		}

		if f && z == 1 {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		c1 := 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 1 {
				c1++
			}
			if i > 0 && a[i]-a[i-1] > 1 {
				c1 = 2
			}
		}
		if c1 != 1 || a[0]-a[n-1] > 1 {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		c1 := 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 1 {
				c1++
			}
		}
		if c1 != 1 || a[0]-a[n-1] > 1 {
			Fprintln(out, "NO")
			continue
		}
		for i := 1; i < n; i++ {
			if a[i]-a[i-1] > 1 {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1658C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n int
o:
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		p1, mx := -1, 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 1 {
				if p1 >= 0 {
					p1 = n
				} else {
					p1 = i
				}
			}
			if a[i] > mx {
				mx = a[i]
			}
		}
		if p1 < 0 || p1 == n {
			Fprintln(out, "NO")
			continue
		}
		for i := p1 + 1; i < p1+n; i++ {
			d := a[i%n] - a[(i-1)%n]
			if d <= 0 {
				mx++
			}
			if d > 1 || mx > n {
				Fprintln(out, "NO")
				continue o
			}
		}
		Fprintln(out, "YES")
	}
}

func main() { CF1658C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		n := readInt(ip)
		a := readArrInt(ip)
		f := true
		c1 := 0
		st:=0
		if a[n-1] == 1 {
			c1++
			st=n-1
		}
		for i := 0; i < n-1; i++ {
			//最多只能有一个1
			if a[i] == 1 {
				c1++
			}
			if a[i]==1{
				st=i
			}
		}

		for i:=0;i<n;i++{
			if a[(st+1)%n]-a[st]>1{
				f=false
				break
			}
			st=(st+1)%n
		}
		if f && c1 == 1{
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

const (
	INF = 3e5 + 10
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		a := make([]int, n)

		pos1 := -1
		for p, _ := range a {
			Fscan(in, &a[p])
			if a[p] == 1 {
				pos1 = p
			}
		}

		if pos1 == -1 {
			Fprintln(out, "NO")
			continue
		}

		flag := true
		r := 2
		for p := (pos1 + 1) % n; p != pos1; p = (p + 1) % n {
			if a[p] < 2 || a[p] > r {
				Fprintln(out, "NO")
				flag = false
				break
			}
			kk := r - a[p]
			r++
			r -= kk

		}

		if flag {
			Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// var N int = 2e5 + 10
// var MOD int64 = 1e9 + 7

func main() {

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		c := make([]int, n)
		for i := 0; i < n; i++ {
			Fscan(in, &c[i])
		}
		pos := -1
		for i, v := range c {
			if v == 1 {
				pos = i
			}
		}
		if pos == -1 {
			Fprintln(out, "NO")
			continue
		}
		ok := true
		for i := (pos + 1) % n; i != pos; i = (i + 1) % n {
			prv := (i - 1 + n) % n
			ok = ok && c[i] <= c[prv]+1
			ok = ok && c[i] >= 2
		}
		if ok {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

const mod = 998244353

// 1 2 3 4
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		var one int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
			if nums[i] == 1 {
				one++
			}
		}
		if one != 1 {
			fmt.Fprintln(out, "NO")
			continue
		}
		ok := true
		nums = append(nums, nums[0])
		for i := 0; i < n; i++ {
			if nums[i+1]-nums[i] > 1 {
				ok = false
				break
			}
		}
		if ok {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	writer  = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
)

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func _int() int {
	s := _string()
	i := 0
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int(r - '0')
	}
	return i
}

func _int64() int64 {
	s := _string()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func _string() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	for t := _int(); t > 0; t-- {
		n := _int()
		c := make([]int, n)
		for i := range c {
			c[i] = _int()
		}
		res := sol(n, c)
		if res {
			printf("YES\n")
		} else {
			printf("NO\n")
		}
	}
}

func sol(n int, c []int) bool {
	if count1(c) != 1 {
		return false
	}
	for i := range c {
		curr := c[i]
		next := c[(i+1)%n]
		if next-curr > 1 {
			return false
		}
	}
	return true
}

func count1(c []int) int {
	res := 0
	for _, i := range c {
		if i == 1 {
			res += 1
		}
	}
	return res
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		front := n - pos

		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i < pos && arr[i] > front+1 {
				ok = false
				break
			} else if i != n-1 && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i < pos && arr[i] > pos+1 {
				ok = false
				break
			} else if i != n-1 && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		front := n - (n - pos - 1) - 1
		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i < pos && arr[i] > front {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		front := n - pos - 1
		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i < pos && arr[i] > front {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		front := n - pos - 1
		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n-1 && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			} else if i < pos && arr[i] > front {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt, pos := 0, 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
				pos = i
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
		}
		front := n - pos - 1
		ok := true
		for i := 0; i < n; i++ {
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n-1 && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			} else if i < pos && arr[i] > front {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		ok := true
		arr = append([]int{arr[0] - 1}, arr...)
		for i := 1; i <= n; i++ {
			if arr[i] == 1 {
				cnt++
			}
			if arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if cnt != 1 {
			ok = false
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		ok := true
		arr = append([]int{arr[0] - 1}, arr...)
		for i := 1; i <= n; i++ {
			if arr[i] == 1 && i != 0 {
				cnt++
			}
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if cnt != 1 {
			ok = false
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		ok := true
		for i := range arr {
			if arr[i] == 1 {
				cnt++
			}
			if i > 0 && arr[i]-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n-1 && arr[i] == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if cnt != 1 {
			ok = false
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
			}
		}
		if cnt != 1 {
			fmt.Println("NO")
			continue
		}
		ok := true
		for i, v := range arr {
			if i > 0 && v-arr[i-1] > 1 {
				ok = false
				break
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				ok = false
				break
			}
		}
		if ok {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
			}
		}
		if cnt != 1 {
			fmt.Fprintln(out, "NO")
			continue
		}
		for i, v := range arr {
			if i > 0 && v-arr[i-1] > 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		cnt := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] == 1 {
				cnt++
			}
		}
		if cnt > 1 {
			fmt.Fprintln(out, "NO")
			continue
		}
		for i, v := range arr {
			if i > 0 && v-arr[i-1] > 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		cnt := 0
		for i, v := range arr {
			if v == 1 {
				cnt++
			}
			if i > 0 && (v-arr[i-1] > 1 || cnt > 1) {
				fmt.Fprintln(out, "NO")
				goto h1
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		cnt := 0
		for i, v := range arr {
			if v == 1 {
				cnt++
			}
			if i > 0 && (v-arr[i-1] > 1 || v-arr[i-1] == 0 || cnt > 1) {
				fmt.Fprintln(out, "NO")
				goto h1
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	CF1658C(os.Stdin, os.Stdout)
}

func CF1658C(_r io.Reader, _w io.Writer) {
	in, out := bufio.NewReader(_r), bufio.NewWriter(_w)
	defer out.Flush()
	c := 0
	fmt.Fscan(in, &c)
	for ; c > 0; c-- {
		n := 0
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		cnt := 0
		for i, v := range arr {
			if v == 1 {
				cnt++
			}
			if i > 0 && (v-arr[i-1] > 1 || v-arr[i-1] == 0) {
				fmt.Fprintln(out, "NO")
				goto h1
			} else if i != n-1 && v == n && arr[i+1] != 1 {
				fmt.Fprintln(out, "NO")
				goto h1
			}
		}
		fmt.Fprintln(out, "YES")
	h1:
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
NO
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[:oneIdx]
	for i, num := range lftArr {
		if i > 0 && num-lftArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	if c[0]-c[n-1] > 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	rhtArr := c[oneIdx:]
	for i, num := range rhtArr {
		if i > 0 && num-rhtArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
NO
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[:oneIdx]
	for i, num := range lftArr {
		if num > i+oneIdx || i > 0 && num-lftArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	if c[0]-c[n-1] > 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	rhtArr := c[oneIdx:]
	for i, num := range rhtArr {
		if num > i+1 || i > 0 && num-rhtArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
NO
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[:oneIdx]
	for i, num := range lftArr {
		if num > i+oneIdx || i > 0 && num-lftArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	rhtArr := c[oneIdx:]
	for i, num := range rhtArr {
		if num > i+1 || i > 0 && num-rhtArr[i-1] > 1 {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
NO
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[:oneIdx]
	for i, num := range lftArr {
		if num > i+oneIdx || i > 0 && num < lftArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	rhtArr := c[oneIdx:]
	for i, num := range rhtArr {
		if num > i+1 || i > 0 && num < rhtArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
3
5
2 2 2 1 2
4
1 2 3 4
4
4 3 2 1
---
YES
YES
YES
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[:oneIdx]
	for i, num := range lftArr {
		if num > i+oneIdx+1 || i > 0 && num < lftArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	rhtArr := c[oneIdx:]
	for i, num := range rhtArr {
		if num > i+1 || i > 0 && num < rhtArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
1
5
2 2 2 1 2
---
YES
=======
1
2
1 1
---
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1
	oneCnt := 0

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
			oneCnt++
		}
	}
	scanf("\n")

	if oneIdx < 0 || oneCnt != 1 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[oneIdx:]
	for i, num := range lftArr {
		if num > i+1 || i > 0 && num < lftArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	rhtArr := c[:oneIdx]
	for i, num := range rhtArr {
		if num > i+oneIdx+1 || i > 0 && num < rhtArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

/*
1
5
2 2 2 1 2
---
YES
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)
	oneIdx := -1

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneIdx = i
		}
	}
	scanf("\n")

	if oneIdx < 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	lftArr := c[oneIdx:]
	for i, num := range lftArr {
		if num > i+1 || i > 0 && num < lftArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	rhtArr := c[:oneIdx]
	for i, num := range rhtArr {
		if num > i+oneIdx+1 || i > 0 && num < rhtArr[i-1] {
			fmt.Fprintln(writer, "NO")
			return
		}
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int64) int64 {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int64) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
)

/*
2
5
2 2 3 1 2
4
3 1 2 2
---
YES
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)

	oneCnt := -1
	oneIdx := -1

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneCnt++
			oneIdx = i
		}
	}
	scanf("\n")

	if oneCnt != 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx+1 < n && c[oneIdx+1] != 2 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if !sort.IntsAreSorted(c[oneIdx:]) {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx > 1 && sort.IsSorted(sort.Reverse(sort.IntSlice(c[:oneIdx]))) {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx > 0 && c[oneIdx-1] < 2 {
		fmt.Fprintln(writer, "NO")
		return
	}

	max := c[maxIdx(c...)]

	if max > n {
		fmt.Fprintln(writer, "NO")
		return
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
)

/*
2
5
2 2 3 1 2
4
3 1 2 2
---
YES
NO
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)

	oneCnt := -1
	oneIdx := -1

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneCnt++
			oneIdx = i
		}
	}
	scanf("\n")

	if oneCnt != 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx+1 < n && c[oneIdx+1] != 2 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if !sort.IntsAreSorted(c[oneIdx:]) {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx > 1 && sort.IsSorted(sort.Reverse(sort.IntSlice(c[:oneIdx]))) {
		fmt.Fprintln(writer, "NO")
		return
	}

	max := c[maxIdx(c...)]

	if max > n {
		fmt.Fprintln(writer, "NO")
		return
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
)

/*

 */

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	c := make([]int, n)

	oneCnt := -1
	oneIdx := -1

	for i := range c {
		scanf("%d", &c[i])

		if c[i] == 1 {
			oneCnt++
			oneIdx = i
		}
	}
	scanf("\n")

	if oneCnt != 0 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx+1 < n && c[oneIdx+1] != 2 {
		fmt.Fprintln(writer, "NO")
		return
	}

	if !sort.IntsAreSorted(c[oneIdx:]) {
		fmt.Fprintln(writer, "NO")
		return
	}

	if oneIdx != 0 && sort.IsSorted(sort.Reverse(sort.IntSlice(c[:oneIdx]))) {
		fmt.Fprintln(writer, "NO")
		return
	}

	fmt.Fprintln(writer, "YES")
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		z := -1
		f := true
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				if z >= 0 {
					f = false
				}
				z = i
			}
		}

		if z < 0 {
			f = false
		}

		if f {
			for i := 0; i < n-1; i++ {
				if arr[i+1] == 1 {
					continue
				}

				if abs(arr[i]-arr[i+1]) > 1 {
					f = false
					break
				}
			}
		}

		if n > 1 && arr[n-1] == 1 && arr[0] != 2 {
			f = false
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		z := -1
		f := true
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				if z >= 0 {
					f = false
				}
				z = i
			}
		}

		if z < 0 {
			f = false
		}

		if f {
			for i := 0; i < n-1; i++ {
				if arr[i+1] == 1 {
					continue
				}

				if arr[i] >= arr[i+1] {
					f = false
					break
				}
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		z := -1
		f := true
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				if z >= 0 {
					f = false
				}
				z = i
			}
		}

		if f {
			for i := 0; i < n-1; i++ {
				if arr[i+1] == 1 {
					continue
				}

				if arr[i+1]-arr[i] != 1 {
					f = false
					break
				}
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		var z int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				z = i
			}
		}

		f := true
		for i := 0; i+1 < z; i++ {
			if arr[i] >= arr[i+1] {
				f = false
				break
			}
		}

		if n > 1 && z > 0 && arr[z-1] == 1 {
			f = false
		}

		for i := z; i < n-1; i++ {
			if arr[i] >= arr[i+1] {
				f = false
				break
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		z := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				z = i
			}
		}

		f := true
		for i := 0; i+1 < z; i++ {
			if arr[i] >= arr[i+1] {
				f = false
				break
			}
		}

		for i := z; i < n-1; i++ {
			if arr[i] >= arr[i+1] {
				f = false
				break
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		arr := make([]int, n)
		z := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])

			if arr[i] == 1 {
				z = i
			}
		}

		f := true
		for i := 0; i+1 < z; i++ {
			if arr[i] > arr[i+1] {
				f = false
				break
			}
		}

		for i := z; i < n-1; i++ {
			if arr[i] >= arr[i+1] {
				f = false
				break
			}
		}

		if f {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1658C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n int
	Fscan(in, &T)
	d := T == 5000
	for ti := 1; ti <= T; ti++ {
		Fscan(in, &n)
		a := make([]int, n)
		p1, mx := -1, 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 1 {
				if p1 >= 0 {
					p1 = n
				} else {
					p1 = i
				}
			}
			if a[i] > mx {
				mx = a[i]
			}
		}
		if d && ti == 219 {
			for _, v := range a {
				Fprint(out, v, " ")
			}
			Fprintln(out)
		}
		if p1 < 0 || p1 == n {
			if !d {
				Fprintln(out, "NO")
			}
			continue
		}
		for i := p1 + 1; i < p1+n; i++ {
			if a[i%n] <= a[(i-1)%n] {
				mx++
			}
		}
		if !d {
			if mx > n {
				Fprintln(out, "NO")
			} else {
				Fprintln(out, "YES")
			}
		}
	}
}

func main() { CF1658C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1658C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		p1, mx := -1, 0
		for i := range a {
			Fscan(in, &a[i])
			if a[i] == 1 {
				if p1 >= 0 {
					p1 = n
				} else {
					p1 = i
				}
			}
			if a[i] > mx {
				mx = a[i]
			}
		}
		if p1 < 0 || p1 == n {
			Fprintln(out, "NO")
			continue
		}
		for i := p1 + 1; i < p1+n; i++ {
			if a[i%n] <= a[(i-1)%n] {
				mx++
			}
		}
		if mx > n {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

func main() { CF1658C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1658C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var T, n, pre, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &pre)
		mx, cnt := pre, n
		for ; n > 1; n-- {
			Fscan(in, &v)
			if v > mx {
				mx = v
			} else if v <= pre {
				cnt--
			}
			pre = v
		}
		if mx > cnt {
			Fprintln(out, "NO")
		} else {
			Fprintln(out, "YES")
		}
	}
}

func main() { CF1658C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		n := readInt(ip)
		a := readArrInt(ip)
		f := true
		c1 := 0
		if a[n-1] == 1 {
			c1++
		}
		for i := 0; i < n-1; i++ {
			//最多只能有一个1
			if a[i] == 1 {
				c1++
			}

			//不可能突增
			if a[i]+1 < a[i+1] {
				f = false
				break
			}
		}
		p := 0
		if n == 1 {
			p = -1
		}
		for i := 0; i < n-1; i++ {
			if a[i] == a[i+1]+1 {
				p++
			}
		}
		if f && c1 == 1 && p < n-1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		n := readInt(ip)
		a := readArrInt(ip)
		f := true
		c1 := 0
		if a[n-1] == 1 {
			c1++
		}
		for i := 0; i < n-1; i++ {
			//最多只能有一个1
			if a[i] == 1 {
				c1++
			}

			//不可能突增
			if a[i]+1 < a[i+1] {
				f = false
				break
			}
		}
		p := 0
		for i := 0; i < n-1; i++ {
			if a[i] == a[i+1]-1 {
				p++
			}
		}
		if f && c1 == 1 && p < n-1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}
func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.ParseInt(nStr, 10, 64)
	return n
}
func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}
func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

func main() {
	ip := bufio.NewReader(os.Stdin)
	t := readInt(ip)
	for t > 0 {
		t--
		n := readInt(ip)
		a := readArrInt(ip)
		f := true
		p := 0
		c1 := 0
		if a[n-1] == 1 {
			c1++
		}
		for i := 0; i < n-1; i++ {
			if a[i] == 1 {
				c1++
			}
			if a[i+1] < a[i] {
				p++
			} else if a[i]+1 >= a[i+1] {
				continue
			} else {
				f = false
				break
			}
		}
		if f && p < 2 && c1 == 1 {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

const (
	INF = 3e5 + 10
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		a := make([]int, n)

		pos1 := -1
		posN := -1
		for p, _ := range a {
			Fscan(in, &a[p])
			if a[p] == 1 {
				pos1 = p
			}
			if a[p] == n {
				posN = p
			}
		}

		if pos1 == -1 {
			Fprintln(out, "NO")
			continue
		}

		flag := true
		r := 2
		for p := (pos1 + 1) % n; p != pos1; p = (p + 1) % n {
			if a[p] < 2 || a[p] > r {
				Fprintln(out, "NO")
				flag = false
				break
			}
			r++
		}

		if posN != -1 && flag {
			l := n - 1
			for p := (posN - 1 + n) % n; p != posN; p = (p - 1 + n) % n {
				if a[p] != l {
					Fprintln(out, "NO")
					flag = false
					break
				}
				l--
			}
		}

		if flag {
			Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

const (
	INF = 3e5 + 10
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	Fscan(in, &t)

	for k := 0; k < t; k++ {
		var n int
		Fscan(in, &n)

		a := make([]int, n)

		pos1 := -1
		for p, _ := range a {
			Fscan(in, &a[p])
			if a[p] == 1 {
				pos1 = p
			}
		}

		if pos1 == -1 {
			Fprintln(out, "NO")
			continue
		}

		r := 2
		flag := true
		for p := (pos1 + 1) % n; p != pos1; p = (p + 1) % n {
			if a[p] < 2 || a[p] > r {
				Fprintln(out, "NO")
				flag = false
				break
			}
			r++
		}
		if flag {
			Fprintln(out, "YES")
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func gcd(a, b int) int {
	if a%b == 0 {
		return b
	}
	return gcd(b, a%b)
}

const mod = 998244353

// 1 2 3 4
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		var one int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
			if nums[i] == 1 {
				one++
			}
		}
		if one != 1 {
			fmt.Fprintln(out, "NO")
		}
		ok := true
		nums = append(nums, nums[0])
		for i := 0; i < n; i++ {
			if nums[i+1]-nums[i] > 1 {
				ok = false
				break
			}
		}
		if ok {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}


You have a rectangular chocolate bar consisting of n × m single squares. You want to eat exactly k squares, so you may need to break the chocolate bar. In one move you can break any single rectangular piece of chocolate in two rectangular pieces. You can break only by lines between squares: horizontally or vertically. The cost of breaking is equal to square of the break length.For example, if you have a chocolate bar consisting of 2 × 3 unit squares then you can break it horizontally and get two 1 × 3 pieces (the cost of such breaking is 32 = 9), or you can break it vertically in two ways and get two pieces: 2 × 1 and 2 × 2 (the cost of such breaking is 22 = 4).For several given values n, m and k find the minimum total cost of breaking. You can eat exactly k squares of chocolate if after all operations of breaking there is a set of rectangular pieces of chocolate with the total size equal to k squares. The remaining n·m - k squares are not necessarily form a single rectangular piece.

The first line of the input contains a single integer t (1 ≤ t ≤ 40910) — the number of values n, m and k to process. Each of the next t lines contains three integers n, m and k (1 ≤ n, m ≤ 30, 1 ≤ k ≤ min(n·m, 50)) — the dimensions of the chocolate bar and the number of squares you want to eat respectively.

For each n, m and k print the minimum total cost needed to break the chocolate bar, in order to make it possible to eat exactly k squares.

NoteIn the first query of the sample one needs to perform two breaks:  to split 2 × 2 bar into two pieces of 2 × 1 (cost is 22 = 4),  to split the resulting 2 × 1 into two 1 × 1 pieces (cost is 12 = 1). In the second query of the sample one wants to eat 3 unit squares. One can use exactly the same strategy as in the first query of the sample.

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF598E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	dp := [31][31][51]int{}
	var f func(n, m, k int) int
	f = func(n, m, k int) (ans int) {
		if n*m == k {
			return 0
		}
		v := &dp[n][m][k]
		if *v > 0 {
			return *v
		}
		defer func() { *v = ans }()
		ans = 1e9
		for i := 1; i < m; i++ {
			if n*i >= k {
				ans = min(ans, f(n, i, k)+n*n)
			} else {
				ans = min(ans, f(n, m-i, k-n*i)+n*n)
			}
		}
		for i := 1; i < n; i++ {
			if m*i >= k {
				ans = min(ans, f(i, m, k)+m*m)
			} else {
				ans = min(ans, f(n-i, m, k-m*i)+m*m)
			}
		}
		return
	}
	var t, n, m, k int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m, &k)
		Fprintln(out, f(n, m, k))
	}
}

func main() { CF598E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

var dp [][][]float64

func initDP() {
	n := 30
	m := 30
	dp = make([][][]float64, n+1)
	for i := range dp {
		dp[i] = make([][]float64, m+1)
		for j := range dp[i] {
			dp[i][j] = make([]float64, n*m + 1)
			for idx := i*j + 1; idx <= n*m; idx++ {
				dp[i][j][idx] = math.MaxFloat64
			}
			dp[i][j][0] = 0.0
			dp[i][j][i*j] = 0.0

		}
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			for kI := 1; kI < i*j; kI++ {
				cost := math.MaxFloat64
				if i > 1 {
					for r := 1; r < i; r++ {
						if dp[i-r][j][kI] != math.MaxFloat64 {
							cost = math.Min(cost, float64(j*j)+dp[i-r][j][kI])
						}
					}
					for r := 1; r <= kI/j; r++ {
						if dp[i-r][j][kI - r*j] != math.MaxFloat64 {
							cost = math.Min(cost, float64(j*j)+dp[i-r][j][kI-r*j])
						}
					}

				}
				if j > 1 {
					for r := 1; r <j; r++ {
						if dp[i][j-r][kI] != math.MaxFloat64 {
							cost = math.Min(cost, float64(i*i)+dp[i][j-r][kI])
						}
					}
					for r := 1; r <= kI/i; r++ {
						if dp[i][j-r][kI - r*i] != math.MaxFloat64 {
							cost = math.Min(cost, float64(i*i) + dp[i][j-r][kI-r*i])
						}
					}
				}
				dp[i][j][kI] = cost
			}
		}
	}
}

func getCost(n, m, k int) float64 {
	return dp[n][m][k]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	tStr := scanner.Text()
	t, _ := strconv.Atoi(tStr)

	initDP()

	for i := 0; i < t; i++ {
		scanner.Scan()
		nmkStr := scanner.Text()
		//fmt.Printf("::%v::\n", nmkStr)
		nmkArr := strings.Split(nmkStr, " ")

		n, _ := strconv.Atoi(nmkArr[0])
		m, _ := strconv.Atoi(nmkArr[1])
		k, _ := strconv.Atoi(nmkArr[2])

		fmt.Println(getCost(n, m, k))
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF598E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}

	dp := [31][31][51]int{}
	var f func(n, m, k int) int
	f = func(n, m, k int) (ans int) {
		if n*m == k {
			return
		}
		v := &dp[n][m][k]
		if *v > 0 {
			return *v
		}
		defer func() { *v = ans }()
		ans = 1e9
		for i := 1; i < m; i++ {
			if n*i >= k {
				ans = min(ans, f(n, i, k)+n*n)
				break
			} else {
				ans = min(ans, f(n, m-i, k-n*i)+n*n)
			}
		}
		for i := 1; i < n; i++ {
			if m*i >= k {
				ans = min(ans, f(i, m, k)+m*m)
				break
			} else {
				ans = min(ans, f(n-i, m, k-m*i)+m*m)
			}
		}
		return
	}
	var t, n, m, k int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n, &m, &k)
		Fprintln(out, f(n, m, k))
	}
}

func main() { CF598E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func getCost(n, m, k int) int {

	if n*m == k {
		return 0
	}

	var minIdx, maxIdx int
	if n > m {
		maxIdx = n
		minIdx = m
	} else {
		maxIdx = m
		minIdx = n
	}

	removedDim := (n*m - k) / minIdx
	if removedDim == 0 {
		return (minIdx * minIdx) + 1
	}

	return (minIdx * minIdx) + getCost(maxIdx-removedDim, minIdx, k)
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	tStr := scanner.Text()
	t, _ := strconv.Atoi(tStr)

	for i := 0; i < t; i++ {
		scanner.Scan()
		nmkStr := scanner.Text()
		//fmt.Printf("::%v::\n", nmkStr)
		nmkArr := strings.Split(nmkStr, " ")

		n, _ := strconv.Atoi(nmkArr[0])
		m, _ := strconv.Atoi(nmkArr[1])
		k, _ := strconv.Atoi(nmkArr[2])

		fmt.Println(getCost(n, m, k))
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func getCost(n, m, k int) int {
	dp := make([][][]int, n+1)
	for i := range dp {
		dp[i] = make([][]int, m+1)
		for j := range dp[i] {
			dp[i][j] = make([]int, n*m + 1)
			for idx := i*j + 1; idx <= n*m; idx++ {
				dp[i][j][idx] = math.MaxInt32
			}
			dp[i][j][0] = 0
			dp[i][j][i*j] = 0

		}
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			for kI := 1; kI < i*j; kI++ {
				cost := math.MaxFloat64
				if i > 1 {
					for r := 1; r < i; r++ {
						cost = math.Min(cost, float64(j*j + dp[i-r][j][kI]))
					}
					for r := 1; r <= kI/j; r++ {
						cost = math.Min(cost, float64(j*j + dp[i-r][j][kI - r*j]))
					}

				}
				if j > 1 {
					for r := 1; r <j; r++ {
						cost = math.Min(cost, float64(i*i+dp[i][j-r][kI]))
					}
					for r := 1; r <= kI/i; r++ {
						cost = math.Min(cost, float64(i*i + dp[i][j-r][kI - r*i]))
					}
				}
				dp[i][j][kI] = int(cost)
			}
		}
	}

	//fmt.Println(dp)
	return dp[n][m][k]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	tStr := scanner.Text()
	t, _ := strconv.Atoi(tStr)

	for i := 0; i < t; i++ {
		scanner.Scan()
		nmkStr := scanner.Text()
		//fmt.Printf("::%v::\n", nmkStr)
		nmkArr := strings.Split(nmkStr, " ")

		n, _ := strconv.Atoi(nmkArr[0])
		m, _ := strconv.Atoi(nmkArr[1])
		k, _ := strconv.Atoi(nmkArr[2])

		fmt.Println(getCost(n, m, k))
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MAX = 900

var dp [][][]int

func initDP() {
	n := 30
	m := 30
	dp = make([][][]int, n+1)
	for i := range dp {
		dp[i] = make([][]int, m+1)
		for j := range dp[i] {
			dp[i][j] = make([]int, n*m + 1)
			for idx := i*j + 1; idx <= n*m; idx++ {
				dp[i][j][idx] = MAX
			}
			dp[i][j][0] = 0
			dp[i][j][i*j] = 0

		}
	}

	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			for kI := 1; kI < i*j; kI++ {
				cost := float64(MAX)
				if i > 1 {
					for r := 1; r < i; r++ {
						cost = math.Min(cost, float64(j*j + dp[i-r][j][kI]))
					}
					for r := 1; r <= kI/j; r++ {
						cost = math.Min(cost, float64(j*j + dp[i-r][j][kI - r*j]))
					}

				}
				if j > 1 {
					for r := 1; r <j; r++ {
						cost = math.Min(cost, float64(i*i+dp[i][j-r][kI]))
					}
					for r := 1; r <= kI/i; r++ {
						cost = math.Min(cost, float64(i*i + dp[i][j-r][kI - r*i]))
					}
				}
				dp[i][j][kI] = int(cost)
			}
		}
	}
}

func getCost(n, m, k int) int {
	return dp[n][m][k]
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	tStr := scanner.Text()
	t, _ := strconv.Atoi(tStr)

	initDP()

	for i := 0; i < t; i++ {
		scanner.Scan()
		nmkStr := scanner.Text()
		//fmt.Printf("::%v::\n", nmkStr)
		nmkArr := strings.Split(nmkStr, " ")

		n, _ := strconv.Atoi(nmkArr[0])
		m, _ := strconv.Atoi(nmkArr[1])
		k, _ := strconv.Atoi(nmkArr[2])

		fmt.Println(getCost(n, m, k))
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

func getCost(n, m, k int) int {

	if n*m < k {
		return math.MaxInt32
	}

	if k == 0 || n*m == k {
		return 0
	}

	var minIdx, maxIdx int
	if n > m {
		maxIdx = n
		minIdx = m
	} else {
		maxIdx = m
		minIdx = n
	}

	removedDim := k / minIdx
	if removedDim == 0 {
		return (minIdx * minIdx) + 1
	}

	return (minIdx * minIdx) + int(math.Min(float64(getCost(maxIdx-removedDim, minIdx, k - (removedDim * minIdx))), float64(getCost(maxIdx-removedDim, minIdx, k))))
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	tStr := scanner.Text()
	t, _ := strconv.Atoi(tStr)

	for i := 0; i < t; i++ {
		scanner.Scan()
		nmkStr := scanner.Text()
		//fmt.Printf("::%v::\n", nmkStr)
		nmkArr := strings.Split(nmkStr, " ")

		n, _ := strconv.Atoi(nmkArr[0])
		m, _ := strconv.Atoi(nmkArr[1])
		k, _ := strconv.Atoi(nmkArr[2])

		fmt.Println(getCost(n, m, k))
	}
}


There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $$$1$$$ to $$$n$$$, the junction $$$1$$$ is called the root.A subtree of a junction $$$v$$$ is a set of junctions $$$u$$$ such that the path from $$$u$$$ to the root must pass through $$$v$$$. Note that $$$v$$$ itself is included in a subtree of $$$v$$$.A leaf is such a junction that its subtree contains exactly one junction.The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $$$t$$$ that all light bulbs in the subtree of $$$t$$$ have different colors.Arkady is interested in the following question: for each $$$k$$$ from $$$1$$$ to $$$n$$$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $$$k$$$?

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of junctions in the tree. The second line contains $$$n - 1$$$ integers $$$p_2$$$, $$$p_3$$$, ..., $$$p_n$$$ ($$$1 \le p_i < i$$$), where $$$p_i$$$ means there is a branch between junctions $$$i$$$ and $$$p_i$$$. It is guaranteed that this set of branches forms a tree.

Output $$$n$$$ integers. The $$$i$$$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $$$i$$$.

NoteIn the first example for $$$k = 1$$$ and $$$k = 2$$$ we can use only one color: the junctions $$$2$$$ and $$$3$$$ will be happy. For $$$k = 3$$$ you have to put the bulbs of different colors to make all the junctions happy.In the second example for $$$k = 4$$$ you can, for example, put the bulbs of color $$$1$$$ in junctions $$$2$$$ and $$$4$$$, and a bulb of color $$$2$$$ into junction $$$5$$$. The happy junctions are the ones with indices $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ then.

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

var ed [][]int = make([][]int, 111111)

var sz [111111]int = [111111]int{}

func fun(node int) int {
	sum := 0
	for i := 0; i < len(ed[node]); i++ {
		sum += fun(ed[node][i])
	}

	sz[node] = sum

	if len(ed[node]) == 0 {
		sum = 1
	}
	return sum
}

func main() {
	defer writer.Flush()
	for i := 0; i < 111111; i++ {
		ed[i] = make([]int, 0)
	}
	var n, t int
	scanf("%d\n", &n)
	for i := 2; i <= n; i++ {
		scanf("%d", &t)
		ed[t] = append(ed[t], i)
	}
	fun(1)
	arr := []int{}

	for i := 0; i < n; i++ {
		arr = append(arr, sz[i+1])
	}
	sort.Ints(arr)

	for i := 0; i < n; i++ {
		if arr[i] == 0 {
			printf("%d ", 1)
		} else {
			printf("%d ", arr[i])
		}
	}

}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1056/D

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func levelOrder(n int, p []int) []int {
	graph := make(map[int][]int)
	for i := 2; i <= n; i++ {
		graph[p[i]] = append(graph[p[i]], i)
	}
	queue := []int{1}
	order := []int{}
	for len(queue) > 0 {
		current := queue[0]
		queue = queue[1:]
		order = append(order, current)
		for _, child := range graph[current] {
			queue = append(queue, child)
		}
	}
	return order
}

func Solve(n int, p []int) []int {
	leaf := make([]int, n+1)
	for i := 1; i <= n; i++ {
		leaf[i] = 1
	}
	for _, elem := range p {
		leaf[elem] = 0
	}
	order := levelOrder(n, p)
	for i := n - 1; i >= 0; i-- {
		leaf[p[order[i]]] += leaf[order[i]]
	}
	sort.Ints(leaf)
	return leaf[:n]
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	p := make([]int, n+1)
	for i := 2; i <= n; i++ {
		fmt.Fscanf(reader, "%d", &p[i])
	}

	for _, elem := range Solve(n, p) {
		fmt.Fprintf(writer, "%d ", elem)
	}
	fmt.Fprintf(writer, "\n")
}