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"
)

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"
	"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"
	"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"
	"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"
)

// 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"
)

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"
	"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 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")
		}
	}
}


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 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"
	"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))
	}
}


// 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")
}

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])
		}
	}

}


package main

import "fmt"

var ans []string

func main() {
	var x int64
	_, _ = fmt.Scan(&x)
	e := 0
	for int64(1<<(e+1)) <= x {
		e++
	}
	y := qMul(x, int64(1<<e))

	ans = append(ans, fmt.Sprint(y, " ^ ", x))
	y = y ^ x
	a, b := solve(x, y, int64(1))
	b = -b
	if (b & 1) == 1 {
		a += y
		b += x
	}
	_ = qMul(x, a)
	_ = qMul(y, b)
	ans = append(ans, fmt.Sprint(a*x, " ^ ", b*y))

	fmt.Println(len(ans))
	for _, it := range ans {
		fmt.Println(it)
	}
}

func solve(a int64, b int64, c int64) (x, y int64) {
	a_temp, b_temp, c_temp := a, b, c
	a = (a%b + b) % b
	c = (c%b + b) % b
	d := exGcd(a, b, &x, &y)
	mod := b / d
	x = x * (c / d)
	x = (x%mod + mod) % mod
	y = (c_temp - a_temp*x) / b_temp
	return x, y

}

func exGcd(a int64, b int64, x *int64, y *int64) int64 {
	var x0, y0, x1, y1 int64 = 1, 0, 0, 1
	r := (a%b + b) % b
	q := (a - r) / b
	*x, *y = 0, 1
	for r > 0 {
		*x = x0 - q*x1
		*y = y0 - q*y1
		x0, y0 = x1, y1
		x1, y1 = *x, *y
		a = b
		b = r
		r = a % b
		q = (a - r) / b
	}
	return b

}

func qMul(a, b int64) int64 {
	if b == 1 {
		return a
	} else if (b & 1) == 1 {
		temp := qMul(a, b-1)
		ans = append(ans, fmt.Sprint(temp, " + ", a))
		//fmt.Println(temp, " + ", a)
		return temp + a
	} else {
		temp := qMul(a, b>>1)
		ans = append(ans, fmt.Sprint(temp, " + ", temp))
		//fmt.Println(temp, " + ", temp)
		return temp << 1
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1427E(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		x, y uint64
		op   byte
	}

	var x uint64
	Fscan(in, &x)
	ans := []pair{{x, x, '^'}}
	for i := 0; i < 20; i++ {
		ans = append(ans, pair{x << i, x << i, '+'})
	}
	const mx = 45
	b := [mx + 1]uint64{}
o:
	for i := uint64(1); b[0] == 0; i++ {
		a := []uint64{}
		for j, y := mx, x*i; j >= 0; j-- {
			if y>>j&1 > 0 {
				if b[j] == 0 {
					b[j] = y
					y = uint64(0)
					for k := i; k > 0; k &= k - 1 {
						v := x << bits.TrailingZeros64(k)
						ans = append(ans, pair{y, v, '+'})
						y += v
					}
					for _, v := range a {
						ans = append(ans, pair{y, v, '^'})
						y ^= v
					}
					continue o
				}
				if b[j] > 0 {
					y ^= b[j]
					a = append(a, b[j])
				}
			}
		}
	}
	Fprintln(out, len(ans))
	for _, p := range ans {
		Fprintf(out, "%d %c %d\n", p.x, p.op, p.y)
	}
}

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

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/bits"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1427E(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		x, y uint64
		op   byte
	}

	var x uint64
	Fscan(in, &x)
	ans := []pair{{x, x, '^'}}
	for i := 0; i < 20; i++ {
		ans = append(ans, pair{x << i, x << i, '+'})
	}
	b := [40]uint64{}
o:
	for i := uint64(1); b[0] == 0; i++ {
		a := []uint64{}
		for j, y := 39, x*i; j >= 0; j-- {
			if y>>j&1 > 0 {
				if b[j] == 0 {
					b[j] = y
					y = uint64(0)
					for k := i; k > 0; k &= k - 1 {
						v := x << bits.TrailingZeros64(k)
						ans = append(ans, pair{y, v, '+'})
						y += v
					}
					for _, v := range a {
						ans = append(ans, pair{y, v, '^'})
						y ^= v
					}
					continue o
				}
				if b[j] > 0 {
					y ^= b[j]
					a = append(a, b[j])
				}
			}
		}
	}
	Fprintln(out, len(ans))
	for _, p := range ans {
		Fprintf(out, "%d %c %d\n", p.x, p.op, p.y)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1427E(in io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		x, y int64
		op   byte
	}

	var x int64
	Fscan(in, &x)
	ans := []pair{{x, x, '^'}}
	for i := 0; i < 20; i++ {
		ans = append(ans, pair{x << i, x << i, '+'})
	}
	b := [40]int64{}
o:
	for i := int64(1); b[0] == 0; i++ {
		a := []int64{}
		for j, y := 39, x*i; j >= 0; j-- {
			if y>>j&1 > 0 {
				if b[j] == 0 {
					b[j] = y
					y = int64(0)
					for k := i; k > 0; k &= k - 1 {
						v := k & -k * x
						ans = append(ans, pair{y, v, '+'})
						y += v
					}
					for _, v := range a {
						ans = append(ans, pair{y, v, '^'})
						y ^= v
					}
					continue o
				}
				if b[j] > 0 {
					y ^= b[j]
					a = append(a, b[j])
				}
			}
		}
	}
	Fprintln(out, len(ans))
	for _, p := range ans {
		Fprintf(out, "%d %c %d\n", p.x, p.op, p.y)
	}
}

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

package main

import "fmt"

var ans []string

func main() {
	var x int64
	_, _ = fmt.Scan(&x)
	e := 0
	for int64(1<<(e+1)) <= x {
		e++
	}
	y := qMul(x, int64(1<<e))

	ans = append(ans, fmt.Sprint(y, " ^ ", x))
	y = y ^ x
	a, b := solve(x, y, int64(1))
	b = -b
	if (b&1) == 1 {
		a += y
		b += x
	}
	_ = qMul(x, a)
	_ = qMul(y, b)
	ans = append(ans, fmt.Sprint(a*x, " ^ ", b*y))

	fmt.Println(len(ans))
	for _, it := range ans {
		fmt.Println(it)
	}
}

func solve(a int64, b int64, c int64) (x, y int64) {
	a_temp, b_temp, c_temp := a, b, c
	a = (a%b + b) % b
	c = (c%b + b) % b
	d := exGcd(a, b, &x, &y)
	mod := b / d
	x = x * (c / d)
	x = (x%mod + mod) % mod
	y = (c_temp - a_temp*x) / b_temp
	return x, y

}

func exGcd(a int64, b int64, x *int64, y *int64) int64 {
	var x0, y0, x1, y1 int64 = 1, 0, 0, 1
	r := (a%b + b) % b
	q := (a - r) / b
	*x, *y = 0, 1
	for r > 0 {
		*x = x0 - q*x1
		*y = y0 - q*y1
		x0, y0 = x1, y1
		x1, y1 = *x, *y
		a = b
		b = r
		r = a % b
		q = (a - r) / b
	}
	return b

}

func qMul(a, b int64) int64 {
	if b == 1 {
		return a
	} else if (b & 1) == 1 {
		temp := qMul(a, b-1)
		ans = append(ans, fmt.Sprint(temp, " + ", b))
		return temp + b
	} else {
		temp := qMul(a, b>>1)
		ans = append(ans, fmt.Sprint(temp, " + ", temp))
		return temp << 1
	}
}

package main

import "fmt"

var ans []string

func main() {
	var x int64
	_, _ = fmt.Scan(&x)
	e := 0
	for int64(1<<(e+1)) <= x {
		e++
	}
	y := qMul(x, int64(1<<e))

	ans = append(ans, fmt.Sprint(y, " ^ ", x))
	y = y ^ x
	a, b := solve(x, y, int64(1))
	b = -b
	_ = qMul(x, a)
	_ = qMul(y, b)
	ans = append(ans, fmt.Sprint(a*x, " ^ ", b*y))

	fmt.Println(len(ans))
	for _, it := range ans {
		fmt.Println(it)
	}
}

func solve(a int64, b int64, c int64) (x, y int64) {
	a_temp, b_temp, c_temp := a, b, c
	a = (a%b + b) % b
	c = (c%b + b) % b
	d := exGcd(a, b, &x, &y)
	mod := b / d
	x = x * (c / d)
	x = (x%mod + mod) % mod
	fmt.Println(c_temp, a_temp, x, b_temp)
	y = (c_temp - a_temp*x) / b_temp
	return x, y

}

func exGcd(a int64, b int64, x *int64, y *int64) int64 {
	var x0, y0, x1, y1 int64 = 1, 0, 0, 1
	r := (a%b + b) % b
	q := (a - r) / b
	*x, *y = 0, 1
	for r > 0 {
		*x = x0 - q*x1
		*y = y0 - q*y1
		x0, y0 = x1, y1
		x1, y1 = *x, *y
		a = b
		b = r
		r = a % b
		q = (a - r) / b
	}
	return b

}

func qMul(a, b int64) int64 {
	if b == 1 {
		return a
	} else if (b & 1) == 1 {
		temp := qMul(a, b-1)
		ans = append(ans, fmt.Sprint(temp, " + ", b))
		return temp + b
	} else {
		temp := qMul(a, b>>1)
		ans = append(ans, fmt.Sprint(temp, " + ", temp))
		return temp << 1
	}
}

package main

import "fmt"

var ans []string

func main() {
	var x int64
	_, _ = fmt.Scan(&x)
	e := 0
	for int64(1<<(e+1)) <= x {
		e++
	}
	y := qMul(x, int64(1<<e))

	ans = append(ans, fmt.Sprint(y, " ^ ", x))
	y = y ^ x
	a, b := solve(x, y, int64(1))
	b = -b
	_ = qMul(x, a)
	_ = qMul(y, b)
	ans = append(ans, fmt.Sprint(a*x, " ^ ", b*y))

	fmt.Println(len(ans))
	for _, it := range ans {
		fmt.Println(it)
	}
}

func solve(a int64, b int64, c int64) (x, y int64) {
	a_temp, b_temp, c_temp := a, b, c
	a = (a%b + b) % b
	c = (c%b + b) % b
	d := exGcd(a, b, &x, &y)
	mod := b / d
	x = x * (c / d)
	x = (x%mod + mod) % mod
	y = (c_temp - a_temp*x) / b_temp
	return x, y

}

func exGcd(a int64, b int64, x *int64, y *int64) int64 {
	var x0, y0, x1, y1 int64 = 1, 0, 0, 1
	r := (a%b + b) % b
	q := (a - r) / b
	*x, *y = 0, 1
	for r > 0 {
		*x = x0 - q*x1
		*y = y0 - q*y1
		x0, y0 = x1, y1
		x1, y1 = *x, *y
		a = b
		b = r
		r = a % b
		q = (a - r) / b
	}
	return b

}

func qMul(a, b int64) int64 {
	if b == 1 {
		return a
	} else if (b & 1) == 1 {
		temp := qMul(a, b-1)
		ans = append(ans, fmt.Sprint(temp, " + ", b))
		return temp + b
	} else {
		temp := qMul(a, b>>1)
		ans = append(ans, fmt.Sprint(temp, " + ", temp))
		return temp << 1
	}
}


package main

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

func solve() {
	n := readInt()
	m := readInt()
	forbidden := make(map[int]map[int]bool)
	for i := 0; i < n; i++ {
		forbidden[i] = make(map[int]bool)
	}
	for i := 0; i < m; i++ {
		u := readInt() - 1
		v := readInt() - 1
		if v < u {
			u, v = v, u
		}
		forbidden[u][v] = true
		forbidden[v][u] = true
	}
	id := make([]int, n)
	for i := 0; i < n; i++ {
		id[i] = i
	}
	sort.Slice(id, func(i, j int) bool {
		if len(forbidden[id[i]]) == len(forbidden[id[j]]) {
			return id[i] < id[j]
		}
		return len(forbidden[id[i]]) < len(forbidden[id[j]])
	})
	bestIndex := id[0]
	fmt.Println(n - 1)
	for j := 0; j < n; j++ {
		if j != bestIndex {
			fmt.Println(bestIndex + 1, j + 1)
		}
	}
}

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

// IO

func readString() string {
	scanner.Scan()
	return scanner.Text()
}

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

package main
import (
	"fmt"
	//"bufio"
	//"os"
	//"strconv"
)

func main() {
	var n,m,x,y,i,j int
	a:=make([]int,1010)
	fmt.Scan(&n,&m)
	for i=0;i<m;i++ {
		fmt.Scan(&x,&y)
		a[x]=1
		a[y]=1
	}
	fmt.Println(n-1)
	for i=1;i<=n;i++{
		if a[i]==0 {
			break
		}
	}
	for j=1;j<=n;j++ {
		if i!=j {
			fmt.Println(i,j)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, m, a, b, i int
	fmt.Scan(&n, &m)
	restricted := make([]bool, n)
	for i=0; i<m; i++ {
		fmt.Scan(&a, &b)
		restricted[a-1] = true
		restricted[b-1] = true
	}
	for i=0; i<n; i++ {
		if !restricted[i] {
			i++
			break
		}
	}
	fmt.Println(n-1)
	for j:=1; j<=n; j++ {
		if i != j {
			fmt.Println(i, j)
		}
	}
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, m := getI(), getI()
  seen := map[int]bool{}
  for i := 0; i < m; i++ {
    u, v := getI(), getI()
    seen[u] = true
    seen[v] = true
  }
  u := 1
  for seen[u] {
    u++
  }
  writer.WriteString(fmt.Sprintf("%d\n", n-1))
  for v := 1; v <= n; v++ {
    if v == u {
      continue
    }
    writer.WriteString(fmt.Sprintf("%d %d\n", u, v))
  }
}

package main

import "fmt"

func main() {
    var n,m,a,b,p int
    var f [1005]bool
    fmt.Scan(&n,&m)
    for i := 0; i < m; i++ {
        fmt.Scan(&a,&b)
        f[a] = true
        f[b] = true
    }
    for i := 1; i <= n; i++ {
        if !f[i] { p = i; break }
    }
    fmt.Println(n-1)
    for i := 1; i <= n; i++ {
        if i != p { fmt.Println(p,i) }
    }
}

// 330B-mic
package main

import (
    "fmt"
)

func main() {
    var n, m, x, y int
    fmt.Scan(&n, &m)
    a := make([]int, n+1)
    for i := 0; i < n+1; i++ {
        a[i] = i
    }
    for i := 0; i < m; i++ {
        fmt.Scan(&x, &y)
        a[x] = 0
        a[y] = 0
    }
    for i := 0; i < n+1; i++ {
        if a[i] != 0 {
            fmt.Println(n - 1)
            for j := 1; j < n+1; j++ {
                if j != a[i] {
                    fmt.Print(a[i])
                    fmt.Print(" ")
                    fmt.Print(j)
                    fmt.Println()
                }
            }
            break
        }
    }

}

package main

import (
	"bufio"
	"fmt"
	"io"
	"math/rand"
	"os"
)

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func solution(reader io.Reader, writer io.Writer) {
	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)

	a := make([]bool, n)
	for i := 0; i < m; i++ {
		var l, r int
		fmt.Fscanf(reader, "%d %d\n", &l, &r)
		l--
		r--
		a[l] = true
		a[r] = true
	}

	var s int
	for i := 0; i < n; i++ {
		if !a[i] {
			s = i
			break
		}
	}

	fmt.Println(n - 1)
	for i := 0; i < n; i++ {
		if i != s {
			fmt.Println(i + 1, s + 1)
		}
	}
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}