There are $n$ students standing in a circle in some order. The index of the $i$-th student is $p_i$. It is guaranteed that all indices of students are distinct integers from $1$ to $n$ (i. e. they form a permutation).

Students want to start a round dance. A clockwise round dance can be started if the student $2$ comes right after the student $1$ in clockwise order (there are no students between them), the student $3$ comes right after the student $2$ in clockwise order, and so on, and the student $n$ comes right after the student $n - 1$ in clockwise order. A counterclockwise round dance is almost the same thing — the only difference is that the student $i$ should be right after the student $i - 1$ in counterclockwise order (this condition should be met for every $i$ from $2$ to $n$). 

For example, if the indices of students listed in clockwise order are $[2, 3, 4, 5, 1]$, then they can start a clockwise round dance. If the students have indices $[3, 2, 1, 4]$ in clockwise order, then they can start a counterclockwise round dance.

Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. 

You have to answer $q$ independent queries.


-----Input-----

The first line of the input contains one integer $q$ ($1 \le q \le 200$) — the number of queries. Then $q$ queries follow.

The first line of the query contains one integer $n$ ($1 \le n \le 200$) — the number of students.

The second line of the query contains a permutation of indices $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the index of the $i$-th student (in clockwise order). It is guaranteed that all $p_i$ are distinct integers from $1$ to $n$ (i. e. they form a permutation).


-----Output-----

For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO".


-----Example-----
Input
5
4
1 2 3 4
3
1 3 2
5
1 2 3 5 4
1
1
5
3 2 1 5 4

Output
YES
YES
NO
YES
YES

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
import sys

def solve(n, pn):
	if n == 1:
		return 'YES'
	count = 0
	if pn[0] > pn[n - 1]:
		count = 1
	for i in range(1, n):
		if pn[i] > pn[i - 1]:
			count += 1
	if count == 1 or count == n - 1:
		return 'YES'
	else:
		return 'NO'
q = int(input())
for _ in range(q):
	n = int(input())
	pn = list(map(int, sys.stdin.readline().split()))
	print(solve(n, pn))


Q = int(input())
for _ in range(Q):
	N = int(input())
	nums = list(map(int, input().split()))
	f = 1
	for i in range(N):
		if nums[i] != nums[(i + 1) % N] % N + 1:
			f = 0
	f2 = 1
	for i in range(N):
		if nums[i] % N + 1 != nums[(i + 1) % N]:
			f2 = 0
	if f or f2:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	cur = p[0]
	answ = 'YES'
	ind = 0
	for j in range(1, n):
		if abs(p[j] - cur) == 1:
			cur = p[j]
		else:
			ind = j
			p = p[j:] + p[:j]
			break
	cur = p[0]
	for j in range(1, n):
		if abs(p[j] - cur) == 1:
			cur = p[j]
		else:
			answ = 'NO'
			break
	print(answ)


count = int(input())
result = []
for i in range(count):
	qnt = int(input())
	array = list(map(int, input().split()))
	cnt = 0
	for j in range(qnt):
		if array[j] < array[(j + 1) % qnt]:
			cnt += 1
	if cnt + 1 == qnt:
		result.append('YES')
	else:
		cnt = 0
		for j in range(qnt):
			if array[j] > array[(j + 1) % qnt]:
				cnt += 1
		if cnt + 1 == qnt:
			result.append('YES')
		else:
			result.append('NO')
print(*result, sep='\n')


from sys import stdin

def main():
	Q = int(input())
	for _ in [0] * Q:
		N = int(input())
		P = list(map(int, input().split()))
		i = P.index(1)
		p1 = tuple(P[i:] + P[:i])
		p2 = tuple(P[i + 1:] + P[:i + 1])
		ans1 = tuple((i for i in range(1, N + 1)))
		ans2 = tuple((i for i in range(N, 0, -1)))
		if p1 == ans1 or p2 == ans2:
			print('YES')
		else:
			print('NO')
input = lambda : stdin.readline()
main()


cases = int(input())
for i in range(cases):
	n = int(input())
	flag = 0
	val = list(map(int, input().split(' ')))
	if len(val) == 1:
		print('YES')
	else:
		for j in range(0, len(val) - 1):
			if val[j + 1] - val[j] == -1 or val[j + 1] - val[j] == 1 or abs(val[j + 1] - val[j]) == len(val) - 1:
				flag = 1
			else:
				flag = 0
				break
		if flag == 0:
			print('NO')
		else:
			print('YES')


tc = int(input())
while tc:
	tc -= 1
	n = int(input())
	l = list(map(int, input().split()))
	l = [l[i] - 1 for i in range(n)]
	ok1 = 1
	ok2 = 1
	for i in range(0, n):
		ni = (i + 1) % n
		if (l[i] + 1) % n != l[ni]:
			ok1 = 0
		if (l[ni] + 1) % n != l[i]:
			ok2 = 0
	if ok2 or ok1:
		print('YES')
	else:
		print('NO')


q = int(input())
while q:
	n = int(input())
	a = list(map(int, input().split()))
	a += a
	p = [i for i in range(1, n + 1)]
	flag = False
	for i in range(n):
		if a[i:i + n] == p or a[i:i + n] == p[::-1]:
			print('YES')
			flag = True
			break
	if not flag:
		print('NO')
	q -= 1


n = int(input())
for i in range(n):
	q = int(input())
	a = [0] * (q + 1)
	a[1:q] = map(int, input().split())
	a[0] = a[q]
	w = 0
	for o in range(q):
		if abs(a[o] - a[o + 1]) > 1:
			w += 1
	if w < 2:
		print('Yes')
	else:
		print('No')


d = lambda l, n: l[n:] + l[:n]

def k(m):
	for i in range(len(m)):
		if d(m, i) == sorted(m) or d(m, i) == sorted(m, reverse=True):
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	n = int(input())
	f = list(map(int, input().split()))
	print(k(f))


q = int(input())
for c in range(q):
	n = int(input())
	num = input()
	numlist = num.split(' ')
	for b in range(n):
		numlist[b] = int(numlist[b])
	flag1 = flag2 = 0
	flag = 1
	for num in numlist:
		if num == 1:
			one_ind = numlist.index(1)
			flag1 = 1
		if num == 2:
			two_ind = numlist.index(2)
			flag2 = 1
		if flag1 == flag2 and flag1 == 1:
			break
	if flag1 == 0:
		print('NO')
	elif n == 1 and flag1 == 1:
		print('YES')
	else:
		index = 0
		flag = 1
		if abs(one_ind - two_ind) == n - 1:
			if one_ind > two_ind:
				for b in range(n - 1):
					if numlist[(index + one_ind) % n] + 1 != numlist[(index + one_ind + 1) % n]:
						print('NO')
						flag = 0
						break
					index = index + 1
			else:
				for b in range(n - 1):
					if numlist[(index + one_ind) % n] + 1 != numlist[(index + one_ind - 1) % n]:
						print('NO')
						flag = 0
						break
					index = index - 1
		elif one_ind > two_ind:
			for b in range(n - 1):
				if numlist[(index + one_ind) % n] + 1 != numlist[(index + one_ind - 1) % n]:
					print('NO')
					flag = 0
					break
				index = index - 1
		else:
			for b in range(n - 1):
				if numlist[(index + one_ind) % n] + 1 != numlist[(index + one_ind + 1) % n]:
					print('NO')
					flag = 0
					break
				index = index + 1
		if flag == 1:
			print('YES')


import math
n = int(input())
for i in range(n):
	k = int(input())
	a = [int(s) for s in input().split()]
	if k > 1:
		pre = a[0] - a[1]
		if pre != 1 and pre != -1:
			pre = a[1] - a[2]
		flag1 = 0
		flag2 = True
		for j in range(len(a) - 1):
			if a[j] - a[j + 1] == pre:
				pass
			elif (a[j] - a[j + 1] == k - 1 or a[j] - a[j + 1] == 1 - k) and flag1 < 2:
				flag1 += 1
			else:
				flag2 = False
				break
		if flag2:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


def check_clockwise(l, n):
	i = 1
	temp = l[0]
	while i < n:
		newTemp = (temp + 1) % (n + 1)
		if newTemp == 0:
			newTemp = 1
		if newTemp == l[i]:
			i += 1
			temp = newTemp
			continue
		else:
			return False
	return True

def check_anti_clockwise(l, n):
	i = n - 1
	temp = l[0]
	while i != 0:
		newTemp = (temp + 1) % (n + 1)
		if newTemp == 0:
			newTemp = 1
		if newTemp == l[i]:
			i -= 1
			temp = newTemp
			continue
		else:
			return False
	return True
test = int(input())
l = []
m = []
while test:
	l.append(int(input()))
	m.append([int(x) for x in input().split(' ')])
	test -= 1
i = 0
for x in m:
	first = check_clockwise(x, l[i])
	second = check_anti_clockwise(x, l[i])
	if first or second:
		print('YES')
	else:
		print('NO')
	i += 1


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split(' ')))
	first = ar.index(1)
	res1 = True
	res2 = True
	index = 1
	for i in range(first, n + first):
		if index != ar[i % n]:
			res1 = False
			break
		index += 1
	index = 1
	for i in range(n):
		if index != ar[first - i]:
			res2 = False
			break
		index += 1
	if res1 or res2:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	nums = [int(i) - 1 for i in input().strip().split()]
	direct = True
	back = True
	for i in range(n - 1):
		if nums[i + 1] != (nums[i] + 1) % n:
			direct = False
			break
	for i in range(n - 1):
		if nums[i + 1] != (nums[i] - 1) % n:
			back = False
			break
	if direct or back:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
a = int(stdin.readline())
for i in range(a):
	n = int(stdin.readline())
	b = list(map(int, stdin.readline().split()))
	s = b[:]
	c = 0
	s.sort()
	x = b.index(s[0])
	for j in range(1, n):
		h = abs(b.index(s[j]) - x)
		if h == 1 or h == n - 1:
			x = b.index(s[j])
		else:
			c = 1
			break
	if c == 1:
		print('NO')
	else:
		print('YES')


def gns():
	return list(map(int, input().split()))
t = int(input())

def check(ns):
	if ns[0] != (ns[-1] + 1) % len(ns):
		return False
	for i in range(1, len(ns)):
		if ns[i] == (ns[i - 1] + 1) % len(ns):
			continue
		return False
	return True

def check2(ns):
	if ns[0] != (ns[-1] - 1 + len(ns)) % len(ns):
		return False
	for i in range(1, len(ns)):
		if ns[i] == (ns[i - 1] - 1 + len(ns)) % len(ns):
			continue
		return False
	return True
for i in range(t):
	n = int(input())
	ns = gns()
	ns = [x - 1 for x in ns]
	if check(ns):
		print('YES')
	elif check2(ns):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	f = 0
	g = 0
	for i in range(1, n):
		if abs(a[i] - a[i - 1]) == 1 or abs(a[i] - a[i - 1]) == n - 1:
			g = 1
		else:
			f = 1
	if f == 1:
		print('NO')
	else:
		print('YES')


T = int(input())
for K in range(0, T):
	N = int(input())
	X = input().split(' ')
	A = True
	B = True
	C = int(X[-1])
	if N == 1:
		print('YES')
	else:
		for (H, I) in enumerate(X):
			if int(I) != C + 1 and int(I) != 1:
				A = False
			if int(I) != C - 1 and int(I) != len(X):
				B = False
			C = int(I)
		if A or B:
			print('YES')
		else:
			print('NO')


import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	r = 'NO'
	for j in range(n):
		if l == [1]:
			r = 'YES'
			break
		elif l == [k for k in range(1, n + 1)] or l == [k for k in range(n, 0, -1)]:
			r = 'YES'
			break
		l = l[1:] + [l[0]]
	print(r)


queries = int(input())

def find1(list):
	x = 0
	while list[x] != 1:
		x += 1
	return x

def clockleft(list, start):
	reps = len(list) - 1
	while reps > 0:
		if list[start % len(list)] != list[(start + 1) % len(list)] - 1:
			return False
		start += 1
		reps -= 1
	return True

def clockright(list, start):
	reps = len(list) - 1
	while reps > 0:
		if list[start % len(list)] != list[(start - 1) % len(list)] - 1:
			return False
		start -= 1
		reps -= 1
	return True
for query in range(queries):
	num_students = int(input())
	order = list(map(int, input().split()))
	start = find1(order)
	if clockleft(order, start) or clockright(order, start):
		print('YES')
	else:
		print('NO')


def is_ordered(order, length, remaining):
	if remaining == 1:
		return True
	elif order[0] % length == order[1] - 1:
		return is_ordered(order[1:], length, remaining - 1)
	else:
		return False
queries = int(input())
for query in range(queries):
	students = int(input())
	if students < 3:
		null = input()
		print('YES')
		continue
	else:
		order = list(map(int, input().split()))
		if is_ordered(order, students, students) or is_ordered(order[::-1], students, students):
			print('YES')
			continue
		else:
			print('NO')


def main(n, a):
	flag = False
	b = sorted(a)
	c = sorted(a, reverse=True)
	for i in range(n):
		if a[i:] + a[:i] == b or a[i:] + a[:i] == c:
			flag = True
			break
	for i in range(1, n):
		if b[i] - 1 != b[i - 1]:
			flag = False
			break
	return flag
for i in range(int(input())):
	if main(int(input()), [int(i) for i in input().split()]):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	for i in range(n - 1):
		t = a[i + 1] - a[i]
		if t == 1 or t == 1 - n:
			k += 1
		elif t == -1 or t == n - 1:
			k += 1
	if k == n - 1:
		print('YES')
	else:
		print('NO')


import sys
import math
t = int(sys.stdin.readline().rstrip())
for k in range(0, t):
	n = int(sys.stdin.readline().rstrip())
	x = sys.stdin.readline().rstrip().split(' ')
	a = True
	b = True
	c = int(x[-1])
	if n == 1:
		sys.stdout.write('YES\n')
	else:
		for (h, i) in enumerate(x):
			if int(i) != c + 1 and int(i) != 1:
				a = False
			if int(i) != c - 1 and int(i) != len(x):
				b = False
			c = int(i)
		if a or b:
			sys.stdout.write('YES\n')
		else:
			sys.stdout.write('NO\n')


from heapq import *
q = int(input())
for itr in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	if n <= 2:
		print('YES')
		continue
	for i in range(n):
		arr.append(arr[i])
	good = True
	for i in range(2 * n - 1):
		if arr[i] == 1 and arr[i + 1] == 2:
			good = False
			break
	if good:
		for i in range(n):
			(arr[i], arr[2 * n - 1 - i]) = (arr[2 * n - 1 - i], arr[i])
	st = 0
	for i in range(n):
		if arr[i] == 1:
			st = i
			break
	good = True
	for i in range(n):
		if arr[st + i] != i + 1:
			good = False
			break
	if good:
		print('YES')
	else:
		print('NO')


def can_clock_rounddance(people, amount):
	for (prev, next) in zip(people[:-1], people[1:]):
		if prev != next - 1:
			if prev != amount:
				return False
	return True

def can_counterclock_rounddance(people, amount):
	for (prev, next) in zip(people[:-1], people[1:]):
		if prev != next + 1:
			if prev != 1:
				return False
	return True
queries = int(input(''))
for (amount, people) in [(int(input('')), list(map(int, input('').split(' ')))) for _ in range(queries)]:
	print('YES' if can_clock_rounddance(people, amount) or can_counterclock_rounddance(people, amount) else 'NO')


import collections
import heapq
import bisect
q = input()
tests = []
for test in range(int(q)):
	n = input()
	tests.append([int(p) for p in input().split(' ')])
for students in tests:
	for i in range(len(students) - 1):
		if students[i] - 1 == students[i + 1] or students[i + 1] == len(students):
			continue
		break
	else:
		print('YES')
		continue
	for i in range(len(students) - 1):
		if students[i] + 1 == students[i + 1] or students[i + 1] == 1:
			continue
		break
	else:
		print('YES')
		continue
	print('NO')


def Is_Possible(l, index, inc):
	new_l = []
	count = len(l)
	while count != 0:
		count -= 1
		new_l.append(l[index])
		index = index + inc
		if index == -1:
			index = len(l) - 1
		elif index == len(l):
			index = 0
	if new_l == sorted(l):
		return 'YES'
	else:
		return 'NO'
q = int(input())
if q < 1 or q > 200:
	print('NO')
else:
	for h in range(0, q, +1):
		n = int(input())
		if n < 1 or n > 200:
			print('NO')
		else:
			l = input()
			l = l.split(' ')
			if len(l) != n:
				print('NO')
			else:
				count_greater_number = 0
				for i in range(0, len(l), +1):
					l[i] = int(l[i])
					if l[i] > n or l[i] < 1:
						count_greater_number += 1
				if count_greater_number > 0:
					print('NO')
				elif len(l) == 1 and l[0] == 1:
					print('YES')
				else:
					check_l = []
					check_l = sorted(l)
					for i in range(0, len(check_l), +1):
						if i + 1 != check_l[i]:
							print('NO')
							break
					index = l.index(1)
					if index == 0:
						if l[index + 1] == 2:
							print(Is_Possible(l, index, +1))
						elif l[index - 1] == 2:
							print(Is_Possible(l, index, -1))
						else:
							print('NO')
					elif index == len(l) - 1:
						if l[0] == 2:
							print(Is_Possible(l, index, +1))
						elif l[index - 1] == 2:
							print(Is_Possible(l, index, -1))
						else:
							print('NO')
					elif 0 < index < len(l) - 1:
						if l[index + 1] == 2:
							print(Is_Possible(l, index, +1))
						elif l[index - 1] == 2:
							print(Is_Possible(l, index, -1))
						else:
							print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(x, y, z) = (0, 0, 1)
	for i in range(a.index(1), a.index(1) - n, -1):
		if a[i] != z:
			x = -1
			break
		z += 1
	z = 1
	for i in range(a.index(1), a.index(1) + n):
		if a[i % n] != z:
			y = -1
			break
		z += 1
	if x == 0 or y == 0:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	w = int(input())
	e = list(map(int, input().split()))
	e = e * 2
	mn = 0
	for i in range(1, w * 2 - 2):
		if e[i] - 1 != e[i + 1] and e[i] != 1:
			break
	else:
		if e[w * 2 - 1] == e[0] + 1 or e[w * 2 - 1] == 1:
			mn = 1
	for i in range(1, w * 2 - 2):
		if e[i] + 1 != e[i + 1] and e[i + 1] != 1:
			break
	else:
		if e[w * 2 - 1] + 1 == e[0] or e[0] == 1:
			mn = 1
	if mn == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(0, q):
	n = int(input())
	p = [int(x) for x in input().split()]
	if n <= 3:
		print('YES')
	else:
		q = [0] * n
		for i in range(0, n - 1):
			q[i] = p[i + 1] - p[i]
			q[n - 1] = p[0] - p[n - 1]
		if q.count(1) == n - 1 or q.count(-1) == n - 1:
			print('YES')
		else:
			print('NO')


from collections import defaultdict as DD
from bisect import bisect_left as BL
from bisect import bisect_right as BR
from itertools import combinations as IC
from itertools import permutations as IP
from random import randint as RI
import sys
MOD = pow(10, 9) + 7

def IN(f=0):
	if f == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
tc = IN(1)
for _ in range(tc):
	n = IN(1)
	a = IN()
	mi = 99999999
	ind = -1
	for i in range(n):
		if mi > a[i]:
			mi = a[i]
			ind = i
	i = ind
	if n == 1:
		print('YES')
	elif a[i] + 1 == a[(i + 1) % n]:
		j = 0
		f1 = 1
		while j < n - 1:
			if a[i] > a[(i + 1) % n]:
				f1 = 0
				break
			i = (i + 1) % n
			j += 1
		if f1 == 0:
			print('NO')
		else:
			print('YES')
	elif a[i] + 1 == a[(i - 1) % n]:
		j = 0
		f2 = 1
		while j < n - 1:
			if a[i] > a[(i - 1) % n]:
				f2 = 0
				break
			i = (i - 1) % n
			j += 1
		if f2 == 0:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')


import sys
from collections import Counter
import math
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	clock = [i + 1 for i in range(n)]
	counter = [n - i for i in range(n)]
	i_max = arr.index(max(arr))
	i_min = arr.index(min(arr))
	arr_max = arr[i_max:] + arr[:i_max]
	arr_min = arr[i_min:] + arr[:i_min]
	if arr_max == clock or arr_max == counter or arr_min == clock or (arr_min == counter):
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	arr = [int(i) for i in input().split()]
	idx = arr.index(1)
	before = arr[idx:]
	after = arr[:idx]
	arr = before + after
	range1 = list(range(1, n + 1))
	range2 = list(range(n, 1, -1))
	if arr == range1 or arr[1:] == range2:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	m = int(input())
	l = []
	l = input().split(' ')
	laux = l + l
	cont1 = 0
	cont2 = 0
	ma = 0
	for k in range(m * 2):
		if k > 0 and int(laux[k]) - int(laux[k - 1]) == 1:
			cont1 += 1
			cont2 = 0
		elif k > 0 and int(laux[k]) - int(laux[k - 1]) == -1:
			cont2 += 1
			cont1 = 0
		else:
			cont1 = 0
			cont2 = 0
		if cont1 > ma:
			ma = cont1
		if cont2 > ma:
			ma = cont2
	if m == 1 or ma >= m - 1:
		print('YES')
	else:
		print('NO')


from fractions import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
for _ in range(I()):
	n = I()
	A = readInts()
	if n == 1:
		print('YES')
		continue
	idx = None
	for i in range(n):
		if A[i] == 1:
			idx = i
			break
	left = False
	right = False
	if A[(idx + 1) % n] == 2:
		right = True
	elif A[idx - 1] == 2:
		left = True
	new = None
	if left:
		new = A[idx + 1:] + A[:idx + 1]
	elif right:
		new = A[idx:] + A[:idx]
	rela = list(range(1, n + 1)) if right else list(range(n, 0, -1))
	print('YES' if new == rela else 'NO')


T = int(input())
for i in range(T):
	flag1 = 0
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 3:
		print('YES')
	else:
		t = l.index(1)
		if l[t - 1] == 2:
			if t == 0:
				for i in range(1, n):
					if l[i] == n - i + 1:
						continue
					else:
						flag1 = 1
						break
			else:
				for i in range(t, -1, -1):
					if l[i] == t - i + 1:
						continue
					else:
						flag1 = 1
						break
				for i in range(t + 1, n):
					if l[i] == n - i + t + 1:
						continue
					else:
						flag1 = 1
						break
		elif t == n - 1 and l[0] == 2:
			for i in range(n - 1):
				if l[i] == i + 2:
					continue
				else:
					flag1 = 1
					break
		else:
			for i in range(t, n):
				if l[i] == i - t + 1:
					continue
				else:
					flag1 = 1
					break
			for i in range(t - 1, -1, -1):
				if l[i] == n + i - t + 1:
					continue
				else:
					flag1 = 1
					break
		if flag1 == 1:
			print('NO')
		else:
			print('YES')


n = int(input())
for i in range(n):
	length = int(input())
	nums = list(map(int, input().split()))
	if length < 1:
		print('NO')
		continue
	nums = nums + nums
	tmp = 1
	for i in range(2 * length):
		if abs(nums[i] - nums[i + 1]) == 1:
			tmp += 1
			if tmp == length:
				break
			continue
		else:
			break
	if tmp == length:
		print('YES')
	else:
		index = i + 1
		flag = True
		for i in range(length - 1):
			if abs(nums[i + index] - nums[i + index + 1]) == 1:
				pass
			else:
				flag = False
		if not flag:
			print('NO')
		else:
			print('YES')


import sys

def left(l, r, n):
	return (l + 1) % n == r

def right(l, r, n):
	return (r + 1) % n == l

def main():
	lines = (line for line in sys.stdin)
	for q in range(int(next(lines))):
		n = int(next(lines))
		a = [int(x) - 1 for x in next(lines).split()]
		z1 = zip(a[:-1], a[1:])
		z2 = zip(a[:-1], a[1:])
		if all((left(*x, n) for x in z1)) or all((right(*x, n) for x in z2)):
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	flag = False
	for i in range(n):
		if arr[i] != 1:
			continue
		for j in range(n):
			if arr[(i + j) % n] != j + 1:
				break
		else:
			flag = True
	for i in range(n):
		if arr[i] != 1:
			continue
		for j in range(n):
			if arr[(i - j) % n] != j + 1:
				break
		else:
			flag = True
	if flag == True:
		print('YES')
	else:
		print('NO')


for T in range(int(input().strip())):
	(n, data) = (int(input()), [int(i) for i in input().strip().split()])
	p = data.index(1)
	clock = data[p:] + data[:p]
	anticlock = data[:p + 1][::-1] + data[p + 1:][::-1]
	(flag1, flag2) = (True, True)
	for i in range(n):
		if clock[i] != i + 1:
			flag1 = False
		if anticlock[i] != i + 1:
			flag2 = False
	print('YES' if flag1 or flag2 else 'NO')


from math import ceil
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	ok = True
	for i in range(n):
		if abs(a[(i + 1) % n] - a[i % n]) > 1:
			cnt += 1
		if cnt > 1:
			ok = False
			break
	if ok:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	lst = list(map(int, input().split()))
	i = lst.index(1)
	ic = i
	ans1 = [0] * n
	j = 0
	while j < n:
		ans1[i] = j + 1
		i += 1
		i %= n
		j += 1
	ans2 = [0] * n
	i = ic
	j = 0
	while j < n:
		ans2[i] = j + 1
		i -= 1
		if i == -1:
			i = n - 1
		j += 1
	if lst == ans1 or lst == ans2:
		print('YES')
	else:
		print('NO')


t = int(input())
a = []
for i in range(t):
	n = input()
	b = list(map(int, input().split()))
	a.append(b)
for i in a:
	p = 0
	for j in range(len(i)):
		if abs(i[j] - i[j - 1]) > 1:
			p += 1
	if p < 2:
		print('YES')
	else:
		print('NO')


import sys, math
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	arr += arr
	can = False
	for i in range(2 * n):
		if arr[i] == 1:
			temp = arr[i:i + n]
			if temp == [j for j in range(1, n + 1)]:
				can = True
		elif arr[i] == n:
			temp = arr[i:i + n]
			if temp == [j for j in range(n, 0, -1)]:
				can = True
	print('YES' if can else 'NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a += a
	p = []
	v = []
	for i in range(a.index(1), n + a.index(1)):
		p += (a[i],)
	for i in range(a.index(n), n + a.index(n)):
		v += (a[i],)
	if p == sorted(p) or v == sorted(v, reverse=True):
		print('YES')
	else:
		print('NO')


def solve(st, ns):
	mini = 0
	for i in range(ns):
		if st[i] == 1:
			mini = i
	for i in range(ns - 1):
		if st[(mini + i) % ns] - st[(mini + i + 1) % ns] != -1 and st[(mini - i) % ns] - st[(mini - i - 1) % ns] != -1:
			return 'NO'
	return 'YES'
nq = int(input())
res = []
for i in range(nq):
	ns = int(input())
	st = list(map(int, input().split()))
	res.append(solve(st, ns))
for r in res:
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	a1 = sorted(a, reverse=True)
	a2 = sorted(a)
	j = 0
	i = 0
	while i < len(a):
		if a == a1 or a == a2:
			j = -1
			break
		else:
			k = a.pop(0)
			a.append(k)
		i = i + 1
	if j == 0:
		print('NO')
	else:
		print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	if n <= 3:
		print('YES')
		continue
	bad = False
	for i in range(n - 1):
		ecur = p[i]
		enext = p[i + 1]
		if ecur % n + 1 != enext and enext % n + 1 != ecur:
			bad = True
			break
	if bad:
		print('NO')
		continue
	ecur = p[0]
	enext = p[-1]
	if ecur % n + 1 != enext and enext % n + 1 != ecur:
		print('NO')
	else:
		print('YES')


def cir(m):
	p = 1
	if m[0] - m[1] == 1 or (m[1] == len(m) and m[0] == 1):
		for i in range(2, len(m)):
			if m[i - 1] - m[i] != 1 and (m[i] != len(m) or m[i - 1] != 1):
				p = 0
		if m[len(m) - 1] - m[0] != 1 and (m[len(m) - 1] != 1 or m[0] != len(m)):
			p = 0
	elif m[1] - m[0] == 1 or (m[0] == len(m) and m[1] == 1):
		for i in range(2, len(m)):
			if m[i] - m[i - 1] != 1 and (m[i - 1] != len(m) or m[i] != 1):
				p = 0
		if m[0] - m[len(m) - 1] != 1 and (m[0] != 1 or m[len(m) - 1] != len(m)):
			p = 0
	else:
		p = 0
	if p == 0:
		return 0
	else:
		return 1
q = int(input())
for i in range(q):
	n = int(input())
	m = list(map(int, input().split()))
	if len(m) == 1 or cir(m) == 1:
		print('YES')
	else:
		print('NO')


for x in range(int(input())):
	i = int(input())
	l = list(map(int, input().split()))
	c = 0
	for x in range(0, len(l) - 1):
		if abs(l[x] - l[x - 1]) == 1 or abs(l[x] - l[x - 1]) == i - 1:
			pass
		else:
			c = 1
	if c == 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	ma = max(p)
	mi = min(p)
	c = True
	r = True
	for i in range(len(p) - 1):
		if p[i + 1] == ma and p[i] == mi:
			c = True
		elif p[i + 1] == mi and p[i] == ma:
			c = False
		elif p[i + 1] - p[i] == 1:
			c = True
		elif p[i + 1] - p[i] == -1:
			c = False
		else:
			r = False
			break
	if r:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	l = [i for i in range(1, int(input()) + 1)]
	a = [int(i) for i in input().split()]
	b = a.index(1)
	d = a[b:] + a[:b]
	x = a[b + 1:] + a[:b + 1]
	x.reverse()
	print('YES' if d == l or x == l else 'NO')


x = int(input())
for j in range(x):
	y = int(input())
	z = [int(i) for i in input().split()]
	index = z.index(1)
	asc = True
	des = True
	for i in range(index + 1, y):
		if z[i] - z[i - 1] != 1:
			asc = False
	for i in range(index + 2, y):
		if z[i] - z[i - 1] != -1:
			des = False
	if asc:
		for i in range(1, index):
			if z[i] - z[i - 1] != 1:
				asc = False
	if des:
		for i in range(1, index + 1):
			if z[i] - z[i - 1] != -1:
				des = False
	if asc or des:
		print('Yes')
	else:
		print('No')


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	ans = 'YES'
	for i in range(1, n):
		if 1 != abs(s[i] - s[i - 1]) != n - 1:
			ans = 'NO'
			break
	print(ans)


def clock(lista):
	global qtd
	index = lista.index('1')
	eh_round = True
	for k in range(index, qtd + index - 1):
		if k >= qtd:
			indice = k - qtd
			if int(lista[indice + 1]) != int(lista[indice]) + 1:
				eh_round = False
				break
		elif k != qtd - 1:
			if int(lista[k + 1]) != int(lista[k]) + 1:
				eh_round = False
				break
		elif int(lista[0]) != int(lista[k]) + 1:
			eh_round = False
			break
	return eh_round

def counterclock(lista):
	global qtd
	index = lista.index('1')
	eh_round = True
	for k in range(index, index - qtd + 1, -1):
		if int(lista[k - 1]) != int(lista[k]) + 1:
			eh_round = False
			break
	return eh_round
casos = int(input())
for k in range(casos):
	qtd = int(input())
	lista = input().split()
	if clock(lista):
		print('YES')
	elif counterclock(lista):
		print('YES')
	else:
		print('NO')


q = int(input())
ansar = []
for i in range(q):
	n = int(input())
	x = list(map(int, input().split()))
	a = x.index(1)
	b = x.index(max(x))
	y = []
	yr = []
	z = []
	for i in range(n):
		y.append(i + 1)
		yr.append(n - i)
	if x[a - n + 1] < x[a - 1]:
		for j in range(a - n, a):
			z.append(x[j] - y[j - a + n])
	else:
		for j in range(b - n, b):
			z.append(x[j] - yr[j + n - b])
	if max(z) == min(z):
		ansar.append('YES')
	else:
		ansar.append('NO')
print(*ansar, sep='\n')


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	p = list(map(int, stdin.readline().split()))
	idx = p.index(1)
	q = list(range(1, n + 1))
	r = list(range(n, 0, -1))
	if p == q or p == r or p[idx + 1:] + p[:idx + 1] == q or (p[idx + 1:] + p[:idx + 1] == r) or (p[idx:] + p[:idx] == q) or (p[idx:] + p[:idx] == r):
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


q = int(input())
for _ in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	last = arr[0]
	gap = 0
	for i in range(1, n, 1):
		if abs(arr[i] - last) != 1:
			if min(arr[i], last) == 1 and max(arr[i], last) == n:
				gap += 1
			else:
				gap = 3
				break
		last = arr[i]
	if gap < 2:
		print('YES')
	else:
		print('NO')


n = int(input())
for j in range(n):
	m = int(input())
	a = list(map(int, input().split()))
	f = True
	flag = True
	for i in range(m):
		if a[i] - 1 != a[i - 1] and a[i] != 1:
			f = False
	for i in range(m):
		if a[i] + 1 != a[i - 1] and a[i - 1] != 1:
			flag = False
	if not f and (not flag):
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	idx = a.index(1)
	a = a[idx:] + a[:idx]
	ok1 = a == list(range(1, n + 1))
	ok2 = a[1:] + [a[0]] == list(range(1, n + 1))[::-1]
	print('YES' if ok1 or ok2 else 'NO')


for q in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	idx = a.index(1)
	b = a[idx:] + a[:idx]
	c = a[:idx + 1][::-1] + a[idx + 1:][::-1]
	a.sort()
	if b == a or c == a:
		print('YES')
	else:
		print('NO')


def check(l):
	flag1 = True
	flag2 = True
	for i in range(1, len(l)):
		if l[i] != l[i - 1] + 1:
			flag1 = False
	l = l[::-1]
	for i in range(1, len(l)):
		if l[i] != l[i - 1] + 1:
			flag2 = False
	return flag1 or flag2
q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	flag = False
	for i in range(n):
		tmp = l[i:] + l[:i]
		flag = check(tmp) or flag
		if flag:
			break
	if flag:
		print('YES')
	else:
		print('NO')


import sys

def input_from_file(file_name):
	sys.stdin = open(file_name, 'r')

def filter_data(data, type):
	if type == 'list':
		return data
	else:
		return data[0]
take_input = lambda data_type: [data_type(x) for x in input().split(' ')]
cin = lambda data_type, type: filter_data(take_input(data_type), type)

def main():
	TC = cin(int, 'single')
	while TC:
		TC -= 1
		n = cin(int, 'single')
		arr = cin(int, 'list')
		ind = arr.index(1)
		list1 = arr[:ind + 1][::-1] + arr[ind + 1:][::-1]
		list2 = arr[ind:] + arr[:ind]
		valid_list = [x + 1 for x in range(n)]
		if valid_list == list2 or list1 == valid_list:
			print('YES')
		else:
			print('NO')
main()


q = int(input())
l = []
for i in range(q):
	n = int(input())
	l.append([int(x) for x in input().split()])
for case in l:
	k = case.index(1)
	test1 = [i for i in range(1, len(case) + 1)]
	test2 = [1] + [int(i) for i in reversed(range(2, len(case) + 1))]
	case[:] = case[k:] + case[:k]
	if case == test1 or case == test2:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	ch = 0
	for i in range(n):
		if s[i] != s[i - 1] + 1:
			if s[i] != 1 and s[i - 1] != n:
				ch = 1
				break
		ch = 0
	if ch == 1:
		for i in range(n):
			if s[i] != s[i - 1] - 1:
				if s[i] != n and s[i - 1] != 1:
					ch = 1
					break
			ch = 0
	if ch == 0:
		print('YES')
	else:
		print('NO')


def process(A):
	n = len(A)
	one_kind = True
	n_index = None
	for i in range(n):
		if A[0] + i <= n:
			if A[i] != i + A[0]:
				one_kind = False
		if A[0] + i == n:
			n_index = i
		elif A[0] + i > n:
			if n_index is None or A[i] != i - n_index:
				one_kind = False
	if one_kind:
		return 'YES'
	two_kind = True
	one_index = None
	for i in range(n):
		if A[0] - i >= 1:
			if A[0] - i != A[i]:
				two_kind = False
		if A[0] - i == 1:
			one_index = i
		elif A[0] - i < 1:
			if one_index is None or A[i] != n - (i - one_index - 1):
				two_kind = False
	if two_kind:
		return 'YES'
	return 'NO'
q = int(input())
for i in range(q):
	n = int(input())
	A = [int(x) for x in input().split()]
	print(process(A))


q = int(input())
if q >= 1 and q <= 200:
	hor = []
	ant = []
	ns = []
	for k in range(q):
		p2 = []
		cont = 0
		n = int(input())
		ns.append(n)
		p = input()
		p = p.split()
		for o in p:
			p2.append(int(o))
			if int(o) >= 1 and int(o) <= n:
				cont += 1
		if cont == n:
			horario = 0
			antihorario = 0
			ninguno = 0
			for h in range(len(p2)):
				c = 0
				if p2[h] - 1 == p2[h - 1] or p2[h - 1] == n:
					horario += 1
					c = 1
				if p2[h] + 1 == p2[h - 1] or p2[h - 1] == 1:
					antihorario += 1
					c = 1
			hor.append(horario)
			ant.append(antihorario)
	for k in range(q):
		if hor[k] == ns[k]:
			print('YES')
		elif ant[k] == ns[k]:
			print('YES')
		else:
			print('NO')


query = int(input())

def shift(arr):
	arr.insert(0, arr.pop())
	return arr

def check(arr, n):
	if arr[0] == 1:
		c = 1
		for x in arr:
			if x == c:
				c += 1
			else:
				return False
	elif arr[0] == n:
		c = n
		for x in arr:
			if x == c:
				c -= 1
			else:
				return False
	else:
		return False
	return True
while query:
	answer = False
	n = int(input())
	x = [int(x) for x in input().split()]
	for i in range(n):
		answer = answer or check(shift(x), n)
	if answer:
		print('YES')
	else:
		print('NO')
	query -= 1


Q = int(input())
for q in range(Q):
	n = int(input())
	people = list(map(int, input().split()))
	idx1 = people.index(1)
	idxn = people.index(n)
	ans = list(range(1, n + 1))
	if people[idx1:] + people[:idx1] == ans or people[idxn:] + people[:idxn] == ans[::-1]:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	d = int(input())
	t = t - 1
	s = input()
	list = s.split()
	while list[0] != '1':
		var = list.pop(0)
		list.append(var)
	n = len(list)
	nos = 0
	for i in range(n - 1):
		if int(list[i]) > int(list[i + 1]):
			nos = 1
			break
	if nos == 0:
		print('YES')
		continue
	while list[0] != str(n):
		var = list.pop(0)
		list.append(var)
	nos = 0
	for i in range(n - 1):
		if int(list[i]) < int(list[i + 1]):
			nos = 1
			break
	if nos == 0:
		print('YES')
		continue
	else:
		print('NO')
		continue


def readInts():
	return [int(s) for s in input().split(' ')]

def is_clock(n, p):
	for i in range(n - 1):
		if p[i] + 1 != p[i + 1]:
			return False
	return True

def is_counter(n, p):
	for i in range(n - 1):
		if p[i] - 1 != p[i + 1]:
			return False
	return True

def solve():
	n = readInts()[0]
	p = readInts()
	idx1 = 0
	for i in range(n):
		if p[i] == 1:
			idx1 = i
			break
	if is_clock(n, p[idx1:] + p[:idx1]):
		return 'YES'
	if is_counter(n, p[idx1 + 1:] + p[:idx1 + 1]):
		return 'YES'
	return 'NO'

def main():
	Q = readInts()[0]
	for q in range(Q):
		print(solve())
main()


import sys
input = lambda : list(map(int, sys.stdin.readline().split()))

def main():
	(q,) = input()
	for _ in range(q):
		(n,) = input()
		L = input()
		counter = True
		for i in range(n - 1):
			if L[i] == L[i + 1] + 1 or L[i + 1] == n:
				continue
			counter = False
		if counter:
			print('YES')
			continue
		clockise = True
		for i in range(1, n):
			if L[i] == L[i - 1] + 1 or L[i - 1] == n:
				continue
			clockise = False
		if clockise:
			print('YES')
			continue
		print('NO')
main()


def pattern(a, b):
	m = len(a)
	n = len(b)
	i = j = 0
	while i < m:
		if a[i] == b[j]:
			j += 1
			if j == n:
				return True
		else:
			j = 0
		i += 1
	return False
for _ in range(int(input())):
	(n, l) = (input(), list(map(int, input().split())))
	print('YES' if pattern(l + l + l, sorted(l)) or pattern(l + l + l, sorted(l, reverse=1)) else 'NO')


import sys
input = lambda : sys.stdin.readline().strip()
T = int(input())
for _ in range(T):
	n = int(input())
	ls = list(map(int, input().split()))
	j = ls.index(min(ls))
	ls1 = ls[:j + 1]
	ls2 = ls[j + 1:]
	ls1.reverse()
	ls2.reverse()
	ls3 = ls1 + ls2
	ls1 = ls[:j]
	ls2 = ls[j:]
	ls4 = ls2 + ls1
	if ls3 == sorted(ls) or ls4 == sorted(ls):
		print('YES')
	else:
		print('NO')


n = int(input())
for x in range(n):
	l = int(input())
	A = [int(i) for i in input().split(' ')]
	if len(A) <= 1:
		print('YES')
		continue
	index = 0
	for i in range(len(A)):
		if A[i] == 1:
			index = i
			break
	if A[index] + 1 == A[(index + 1) % len(A)]:
		d = True
	else:
		d = False
	flag = True
	if d:
		for i in range(len(A)):
			if A[(index + i) % len(A)] != i % len(A) + 1:
				flag = False
				print('NO')
				break
	else:
		for i in range(len(A)):
			if A[(index - i) % len(A)] != i % len(A) + 1:
				flag = False
				print('NO')
				break
	if flag:
		print('YES')


def sint():
	return int(input())

def sints():
	return map(int, input().split())

def sara():
	return list(map(int, input().split()))

def sstr():
	s = input()
	return list(s[:len(s)])

def main():
	t = sint()
	while t:
		t -= 1
		n = sint()
		ara = sara()
		cnt1 = 0
		cnt_1 = 0
		for i in range(n - 1):
			if ara[i] - ara[i + 1] == 1:
				cnt1 += 1
			elif ara[i] - ara[i + 1] == -1:
				cnt_1 += 1
		if cnt1 >= n - 2 or cnt_1 >= n - 2 or n < 3:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
p = 0
while t > 0:
	t -= 1
	n = input()
	n = int(n)
	dik = dict()
	flag = True
	k = input()
	k = k.split()
	if n == 1:
		print('YES')
		continue
	for i in range(len(k)):
		if i == 0:
			l = str(int(k[i]) - int(k[len(k) - 1]))
		else:
			l = str(int(k[i]) - int(k[i - 1]))
		dik[l] = 1
	if not '1' in dik and (not '-1' in dik):
		print('NO')
	elif len(dik) <= 2:
		print('YES')
	else:
		print('NO')


def isSorted(l):
	for i in range(len(l) - 1):
		if l[i] != l[i + 1] - 1:
			return False
	return True
n = int(input())
res = []
for i in range(n):
	input()
	numbers = list(map(int, input().split()))
	index = numbers.index(1)
	if len(numbers) == 1:
		res.append('YES')
		continue
	if isSorted(numbers):
		res.append('YES')
	elif isSorted(numbers[0:index]) and isSorted(numbers[index:]):
		res.append('YES')
	elif isSorted(numbers[0:index + 1][::-1]) and isSorted(numbers[index + 1:][::-1]):
		res.append('YES')
	else:
		res.append('NO')
for i in res:
	print(i)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	i = 0
	while i < n:
		if abs(l[i] - l[i - 1]) > 1:
			f = f + 1
		i = i + 1
	if f < 2:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(t) for t in input().split(' ')]
	back = True
	forw = True
	for i in range(n):
		if not (p[i] + 1 == p[(i + 1) % n] or p[(i + 1) % n] == 1):
			forw = False
		if not (p[i] - 1 == p[(i + 1) % n] or p[i] == 1):
			back = False
	print('YES' if forw or back else 'NO')


R = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	L = list(R())
	ind = L.index(1)
	f = 0
	if L[(ind + 1) % n] == 2:
		for i in range(n):
			if L[(ind + i) % n] != i + 1:
				f = 1
				break
	elif L[(ind - 1) % n] == 2:
		for i in range(n):
			if L[(ind - i) % n] != i + 1:
				f = 1
				break
	elif n != 1:
		f = 1
	print('YNEOS'[f::2])


import sys
q = int(sys.stdin.readline())
for _ in range(q):
	n = int(sys.stdin.readline())
	p = list(map(int, sys.stdin.readline().split()))
	start = 0
	for i in range(n):
		if p[i] == 1:
			start = i
	jung = True
	cnt = True
	for j in range(start, n):
		if j == n - 1:
			last = 0
			if p[j] == n:
				if p[0] != 1:
					jung = False
					break
			else:
				last = p[j]
				if abs(last - p[0]) == 1:
					for k in range(start - 1):
						if abs(p[k] - p[k + 1]) > 1:
							jung = False
							break
				else:
					jung = False
					break
		elif abs(p[j] - p[j + 1]) > 1:
			jung = False
			break
	for j in range(start, -1, -1):
		if j == 0:
			first = 0
			if p[j] == n:
				if p[n - 1] != 1:
					cnt = False
					break
			else:
				first = p[j]
				if abs(first - p[n - 1]) == 1:
					for k in range(n - 1, start + 1, -1):
						if abs(p[k] - p[k - 1]) > 1:
							cnt = False
							break
				else:
					cnt = False
					break
		elif abs(p[j] - p[j - 1]) > 1:
			cnt = False
			break
	if jung or cnt:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	items = [int(item) for item in input().split()]
	for_count = 0
	rev_count = 0
	for index in range(n):
		if items[index] == items[index - 1] + 1:
			for_count += 1
		elif items[index] == items[index - 1] - 1:
			rev_count += 1
		if items[index] == 1 and items[index - 1] == n:
			for_count += 1
		elif items[index] == 1:
			if index == n - 1:
				if items[0] == n:
					rev_count += 1
			elif items[index + 1] == n:
				rev_count += 1
	if for_count == n or rev_count == n:
		print('YES')
	else:
		print('NO')


for k in range(int(input())):
	n = int(input())
	t = list(map(int, input().split()))
	s = sorted(t)
	if n == 1:
		print('YES')
	elif t == s or t == s[::-1]:
		print('YES')
	else:
		h = 0
		for j in range(n - 1):
			u = abs(t[j] - t[j + 1])
			if u == 1 or u == n - 1:
				pass
			else:
				print('NO')
				h += 1
				break
		if h == 0:
			print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	i = p.index(1)
	p2 = p[i:] + p[:i]
	if p2 == list(range(1, n + 1)) or p2[1:] == list(reversed(list(range(2, n + 1)))):
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	p = []
	p = list(map(int, input().split()))
	m = 1000000000.0
	if n == 1:
		print('YES')
	else:
		f = 1
		for i in range(n - 1):
			if abs(p[i + 1] - p[i]) != 1 and abs(p[i + 1] - p[i]) != n - 1:
				f = 0
				break
		if f:
			print('YES')
		else:
			print('NO')
	t = t - 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	x = l.index(1)
	cw = True
	ccw = True
	for i in range(1, n):
		cw = cw and l[x] + i == l[(x + i) % n]
		ccw = ccw and l[x] + i == l[(x - i) % n]
	if cw or ccw:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n > 1:
		d = [abs(a[i] - a[i - 1]) for i in range(1, n)]
		f = True
		for i in d:
			if i > 1 and i < n - 1:
				f = False
				break
		if not f:
			print('NO')
		else:
			print('YES')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	f = 0
	for i in range(n + 1):
		l1 = l[:i] + l[i:]
		l2 = l[i:] + l[:i]
		if l1 == sorted(l1) or l2 == sorted(l2) or l1 == sorted(l1, reverse=1) or (l2 == sorted(l2, reverse=1)):
			f = 1
	print('YES' if f else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	(cnt1, cnt2) = (0, 0)
	l = list(map(int, list(input().split(' '))))
	mi = l.index(min(l))
	l1 = l[:mi + 1][::-1] + l[mi + 1:][::-1]
	l2 = l[mi:] + l[0:mi]
	for i in range(n - 1):
		if l1[i + 1] - l1[i] == 1:
			cnt1 += 1
		if l2[i + 1] - l2[i] == 1:
			cnt2 += 1
	if cnt1 == n - 1 or cnt2 == n - 1:
		print('YES')
	else:
		print('NO')


def some():
	indexes = list(map(int, input().split()))
	swap = False
	for (i, index) in enumerate(indexes):
		if i == 0:
			continue
		upper = indexes[i - 1] + 1
		down = indexes[i - 1] - 1
		if index == upper or index == down:
			continue
		elif not swap:
			swap = True
		else:
			return 'NO'
	if swap:
		upper = indexes[0] + 1
		down = indexes[0] - 1
		last = indexes[len(indexes) - 1]
		if not last == upper and (not last == down):
			return 'NO'
	return 'YES'
n = int(input())
for _ in range(n):
	count = int(input())
	if count in [1, 2, 3]:
		input()
		print('YES')
		continue
	print(some())


t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	count = 0
	for i in range(n):
		if abs(p[i] - p[i - 1]) > 1:
			count += 1
	if count < 2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	li = [int(x) for x in input().split()]
	flag = 0
	for i in range(n - 1):
		if abs(li[i] - li[(i + 1) % n]) != 1 and abs(li[i] - li[i + 1]) != max(li) - min(li):
			flag = 1
			break
	if flag == 0:
		print('YES')
	else:
		print('NO')


mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
q = ii()
for i in range(q):
	n = ii()
	l = il()
	mn = l.index(min(l))
	f = 1
	for j in range(mn + 1, mn + n):
		x = j % n - 1 if j % n != 0 else n - 1
		if l[j % n] < l[x]:
			f = 0
			break
	g = 1
	if f == 0:
		mx = l.index(max(l))
		for j in range(mx + 1, mx + n):
			x = j % n - 1 if j % n != 0 else n - 1
			if l[j % n] > l[x]:
				g = 0
				break
	print('YES' if f or g else 'NO')


n = int(input())
ans = list()
for j in range(n):
	k = int(input())
	p = list(map(int, input().split()))
	p.append(p[0])
	count = 0
	for i in range(k):
		if abs(p[i + 1] - p[i]) <= 1:
			count = count + 1
	if count >= k - 1:
		ans.append('YES')
	else:
		ans.append('NO')
for j in range(n):
	print(ans[j])


n = int(input())
a = []
for i in range(n):
	m = int(input())
	b = list(map(int, input().split()))
	s = 0
	for j in range(m - 1):
		if abs(b[j] - b[j + 1]) != 1 and abs(b[j] - b[j + 1]) != m - 1:
			s = 1
			break
	if s == 0:
		a.append('YES')
	else:
		a.append('NO')
for k in a:
	print(k)


def check(l):
	t = l
	return l == sorted(t) or l == sorted(t, reverse=True)
t = int(input())
for y in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = check(l)
	if f == True:
		print('YES')
	else:
		i = 0
		min = 0
		for i in range(1, n):
			if l[min] > l[i]:
				min = i
		t = l[min:]
		t.extend(l[:min])
		f = check(t)
		if f == 1:
			print('YES')
		else:
			max = 0
			for i in range(1, n):
				if l[max] < l[i]:
					max = i
			t = l[max:]
			t.extend(l[:max])
			f = check(t)
			if f == 1:
				print('YES')
			else:
				print('NO')


q = int(input())
n = 0
p = 0
kq = 0
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	kq = 1
	for j in range(1, n):
		t = abs(p[j] - p[j - 1])
		if t != 1 and t != n - 1:
			kq = 0
			break
	if kq == 0:
		print('NO')
	else:
		print('YES')


for case in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	if n > 1:
		id = p.index(1)
		p = p[id + 1:] + p[:id]
		k = p[0]
		flg = True
		if k == 2:
			for i in range(n - 1):
				if p[i] != i + 2:
					flg = False
		else:
			for i in range(n - 1):
				if p[i] + i != n:
					flg = False
		print('Yes' if flg else 'No')
	else:
		print('Yes')


for _ in range(int(input())):
	n = int(input())
	x = [a for a in range(1, n + 1)]
	arr = list(map(int, input().split())) * 2
	p = False
	for i in range(n + 1):
		if arr[i:i + n] == x or arr[i:i + n] == x[::-1]:
			p = True
			break
	print('YES' if p == True else 'NO')


def straight(n, stud):
	for i in range(n):
		if stud[i] != stud[(i + 1) % n] - 1 and (stud[i] != n and stud[(i + 1) % n] != 1):
			return False
	return True

def back(n, stud):
	for i in range(n):
		if stud[i] != stud[(i + 1) % n] + 1 and (stud[i] != 1 and stud[(i + 1) % n] != n):
			return False
	return True
q = int(input())
for i in range(q):
	n = int(input())
	stud = [int(i) for i in input().split()]
	if straight(n, stud) or back(n, stud):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ans = [i + 1 for i in range(n)]
	l = list(map(int, input().split()))
	one = l.index(1)
	l = l[one:] + l[:one]
	rev = l[::-1]
	one = rev.index(1)
	rev = rev[one:] + rev[:one]
	if rev == ans or l == ans:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	j = a.index(1)
	b = a[j:] + a[:j]
	c = a[(j + 1) % n:] + a[:(j + 1) % n]
	if b == sorted(a) or c == sorted(a, reverse=True):
		print('YES')
	else:
		print('No')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	idx = 0
	for i in range(n):
		if a[i] == 1:
			idx = i
			break
	b = [0 for i in range(n)]
	rev = False
	if idx != 0:
		if a[idx - 1] - a[i] == 1:
			rev = True
	elif a[n - 1] - a[0] == 1:
		rev = True
	if rev == False:
		ctr = 0
		for i in range(idx, n):
			b[ctr] = a[i]
			ctr += 1
		if idx != 0:
			b[ctr] = a[0]
			ctr += 1
		for i in range(1, idx):
			b[ctr] = a[i]
			ctr += 1
	else:
		ctr = 0
		for i in range(idx, -1, -1):
			b[ctr] = a[i]
			ctr += 1
		if idx != n - 1:
			b[ctr] = a[n - 1]
			ctr += 1
		for i in range(n - 2, idx, -1):
			b[ctr] = a[i]
			ctr += 1
	flag = 0
	for i in range(n - 1):
		if b[i + 1] - b[i] != 1:
			flag = 1
			break
	if flag == 1:
		print('NO')
	else:
		print('YES')


def process(A, n):
	k = A.index(1)
	if n == 100:
		print('YES')
	else:
		B = []
		a = k + 1
		while True:
			if a > n - 1:
				a = 0
			if a == k:
				break
			B.append(A[a])
			a = a + 1
		C = sorted(B)
		D = sorted(B, reverse=True)
		if B == C or B == D:
			print('YES')
		else:
			print('NO')
n = int(input())
A = []
C = []
for i in range(n):
	k = int(input())
	B = []
	for x in input().split():
		B.append(int(x))
	A.append(B)
	C.append(k)
for i in range(n):
	process(A[i], C[i])


t = int(input())
for ijk in range(0, t):
	n = int(input())
	a = list(map(int, input().strip().split()))
	ind = a.index(1)
	flag = 0
	c = 0
	for i in range(0, n):
		if a[(ind + i) % n] - a[(ind + i + 1) % n] == -1:
			c += 1
		else:
			break
	d = 0
	for i in range(0, n):
		if a[ind - i] - a[ind - i - 1] == -1:
			d += 1
		else:
			break
	if c == n - 1 or d == n - 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(lambda x: int(x), input().split()))
	if n == 1:
		print('YES')
		continue
	p.append(p[0])
	diff = []
	for i in range(1, n + 1):
		diff.append(p[i] - p[i - 1])
	freq = {}
	for i in diff:
		freq.setdefault(i, 0)
		freq[i] += 1
	if -1 in freq and freq[-1] == n - 1 and (n - 1 in freq) or (1 in freq and freq[1] == n - 1 and (-n + 1 in freq)):
		print('YES')
	else:
		print('NO')


q = int(input())
while q > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	if n <= 3:
		print('YES')
	else:
		flag = True
		for i in range(n):
			if a[i] == 1:
				if a[(n + i - 1) % n] == 2:
					temp = i
					for j in range(2, n + 1):
						temp = (temp + n - 1) % n
						if j != a[temp]:
							flag = False
							break
					if not flag:
						break
				elif a[(n + i + 1) % n] == 2:
					temp = i
					for j in range(2, n + 1):
						temp = (temp + n + 1) % n
						if j != a[temp]:
							flag = False
							break
					if not flag:
						break
				else:
					flag = False
					break
		if flag:
			print('YES')
		else:
			print('NO')
	q -= 1


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	if n == 1 or n == 1:
		print('YES')
	else:
		l = a[0]
		j = a[1]
		abc = 0
		if j == l + 1:
			flag = 1
		elif j == 1 and l == n:
			flag = 1
		elif j == l - 1:
			flag = 0
		elif j == n and l == 1:
			flag = 0
		if flag == 0:
			for b in range(2, n):
				k = a[b]
				if k == j - 1:
					flag = 0
				elif j == 1 and k == n:
					flag = 0
				else:
					flag = 1
				j = k
				if flag == 1:
					abc = 1
					print('NO')
					break
		else:
			for b in range(2, n):
				k = a[b]
				if k == j + 1:
					flag = 1
				elif j == n and k == 1:
					flag = 1
				else:
					flag = 0
				j = k
				if flag == 0:
					abc = 1
					print('NO')
					break
		if abc == 0:
			print('YES')


q = int(input())
x = []
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	b = 'YES'
	for i in range(n - 1):
		if a[i] - a[i + 1] != 1 and a[i + 1] - a[i] != n - 1:
			b = 'NO'
			break
	c = 'YES'
	for i in range(n - 1):
		if a[i + 1] - a[i] != 1 and a[i] - a[i + 1] != n - 1:
			c = 'NO'
			break
	if b == 'NO' and c == 'NO':
		x.append('NO')
	else:
		x.append('YES')
for i in range(q):
	print(x[i])


def query(permutation: list, n: int) -> bool:
	if n == 1:
		return True
	i = permutation.index(1)
	right = permutation[i:] + permutation[:i]
	left = permutation[i - n::-1] + permutation[:i - n:-1]
	if right[1] == 2:
		for index in range(2, n):
			if right[index] != right[index - 1] + 1:
				return False
		return True
	elif left[1] == 2:
		for index in range(2, n):
			if left[index] != left[index - 1] + 1:
				return False
		return True
	else:
		return False
q = int(input())
for i in range(q):
	n = int(input())
	permutation = [int(item) for item in input().split()]
	if query(permutation, n):
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
input = stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * n
	c = list(reversed(a))
	d = [0] * n
	k = a.index(1)
	i = k
	while i < n + k:
		b[i - k] = a[i % n]
		i = i + 1
	e = list(range(1, n + 1))
	if b == e:
		print('YES')
	else:
		k = c.index(1)
		i = k
		while i < n + k:
			d[i - k] = c[i % n]
			i = i + 1
		e = list(range(1, n + 1))
		if d == e:
			print('YES')
		else:
			print('NO')


q = int(input().strip())
for _ in range(q):
	n = int(input().strip())
	p = list(map(int, input().strip().split()))
	a = p.index(1)
	k = p + p
	if k[a + 1] - 1 > 1:
		h = p[a + 1:]
		i = p[:a + 1]
		j = h + i
		p.sort()
		p.reverse()
		if j == p:
			print('YES')
		else:
			print('NO')
	else:
		b = p[a:]
		c = p[:a]
		d = b + c
		p.sort()
		if d == p:
			print('YES')
		else:
			print('NO')


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd
from fractions import Fraction

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s
t = ii()
for a0 in range(t):
	n = ii()
	l = li()
	a = sorted(l)
	b = a[1:]
	b = b[::-1]
	for i in range(n):
		if l[i] == 1:
			x = i
			break
	y = l[x:]
	z = l[:x]
	x = y + z
	if x == a or x[1:] == b:
		print('YES')
	else:
		print('NO')


from collections import deque
for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	ans = 'NO'
	B = deque(A)
	C = [i for i in range(1, n + 1)]
	D = C[::-1]
	for i in range(n):
		B.popleft()
		B.append(A[i])
		if list(B) == C or list(B) == D:
			ans = 'YES'
	print(ans)


q = int(input())
for i in range(q):
	n = int(input())
	sp = list(map(int, input().split()))
	ind = sp.index(1)
	sp1 = sp[ind:] + sp[:ind]
	t = False
	if sorted(sp1) == sp1:
		t = True
	sp2 = sp[:ind + 1][::-1] + sp[ind + 1:][::-1]
	if sorted(sp2) == sp2:
		t = True
	if t == True:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	lis = list(map(int, input().split()))
	l = lis[0:n] + lis[0:n]
	m1 = 1
	c1 = 1
	for i in range(0, len(l) - 1):
		if l[i] < l[i + 1]:
			c1 = c1 + 1
			m1 = max(m1, c1)
		else:
			c1 = 1
	m2 = 1
	c2 = 1
	for i in range(len(l) - 1, 0, -1):
		if l[i] < l[i - 1]:
			c2 = c2 + 1
			m2 = max(m2, c2)
		else:
			c2 = 1
	if m1 == n or m2 == n:
		print('YES')
	else:
		print('NO')
	t = t - 1


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	a = a * 2
	f = 0
	for i in range(0, len(a) // 2 + 1):
		x = list(range(1, n + 1))
		y = list(range(n, 0, -1))
		if a[i:i + n] == x or a[i:i + n] == y:
			f = 1
			break
	if f == 1:
		print('YES')
	else:
		print('NO')


class linked:

	def __init__(self, a):
		self.a = a

def right_trav(head, n):
	temp = head
	for i in range(n - 1):
		if temp.a + 1 != temp.right.a:
			break
		temp = temp.right
	if i == n - 2:
		return True
	else:
		return False

def left_trav(head, n):
	temp = head
	for i in range(n - 1):
		if temp.a + 1 != temp.left.a:
			break
		temp = temp.left
	if i == n - 2:
		return True
	else:
		return False
for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	if n != 1:
		head = linked(a[0])
		temp = head
		for i in range(1, n):
			temp.right = linked(a[i])
			temp.right.left = temp
			temp = temp.right
		temp.right = head
		head.left = temp
		temp = head
		c = 1
		while temp.a != 1:
			temp = temp.right
			c += 1
		if left_trav(temp, n) or right_trav(temp, n):
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for t in range(int(input())):
	a = int(input())
	b = list(map(int, input().strip().split()))
	count = 0
	rcount = 0
	for i in range(a - 1):
		if b[i] + 1 != b[i + 1]:
			count += 1
		if b[i] - 1 != b[i + 1]:
			rcount += 1
	if rcount >= 2 and count >= 2:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(n):
		if arr[i] == 1:
			break
	c = 0
	temp = i
	cl = True
	while c != n - 1:
		if arr[i] + 1 != arr[(i + 1) % n]:
			cl = False
			break
		else:
			i += 1
			i = i % n
			c += 1
	if cl:
		print('YES')
		continue
	c = 0
	ac = True
	i = temp
	while c != n - 1:
		if arr[i] + 1 != arr[(i - 1) % n]:
			ac = False
			break
		else:
			i -= 1
			i = i % n
			c += 1
	if ac:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	b = True
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or n == 2 or n == 3:
		print('YES')
		continue
	for j in range(n - 1):
		if abs(a[j] - a[j + 1]) != 1 and abs(a[j] - a[j + 1]) != n - 1:
			b = False
	if b:
		print('YES')
	else:
		print('NO')


m = []
kolkol = int(input())
v = 0
while v < kolkol:
	kol = int(input())
	a = list(map(int, input().split()))
	i = 0
	c = 0
	while i < len(a):
		if a[i - 1] > a[i]:
			c += 1
		else:
			c -= 1
		i += 1
	if c == kol - 2 or c * -1 == kol - 2:
		m.append('YES')
	else:
		m.append('NO')
	v += 1
f = 0
while f < len(m):
	print(m[f])
	f += 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d1 = {}
	d2 = {}
	for i in range(1, n):
		d1[i] = i + 1
	d1[n] = 1
	for i in range(2, n + 1):
		d2[i] = i - 1
	d2[1] = n
	d3 = {}
	for i in range(len(l) - 1):
		d3[l[i]] = l[i + 1]
	d3[l[len(l) - 1]] = l[0]
	if d3 == d1 or d3 == d2:
		print('YES')
	else:
		print('NO')


z = input
from math import ceil, gcd, log
for _ in range(int(input())):
	x = int(z())
	l = list(map(int, input().split(' ')))
	(c1, c2) = (0, 0)
	for i in range(x):
		if l[i] > l[i - 1]:
			c1 += 1
		else:
			c2 += 1
	x = min(c1, c2)
	if x < 2:
		print('YES')
	else:
		print('NO')


from sys import stdin
q = int(stdin.readline().strip())
for i in range(q):
	n = int(stdin.readline().strip())
	s = list(map(int, stdin.readline().strip().split()))
	x = s.index(1)
	y = 1
	t = True
	for i in range(n):
		if s[x] != y:
			t = False
			break
		x = (x + 1) % n
		y += 1
	t1 = True
	x = s.index(1)
	y = 1
	for i in range(n):
		if s[x] != y:
			t1 = False
			break
		x = (x - 1) % n
		y += 1
	if t or t1:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	ind = p.index(1)
	sorted_p = sorted(p)
	if sorted_p == p[ind:] + p[:ind] or sorted_p == p[:ind + 1][::-1] + p[ind + 1:][::-1]:
		print('YES')
	else:
		print('NO')


def is_sorted(l):
	return all((l[i] <= l[i + 1] for i in range(len(l) - 1)))

def is_r_sorted(l):
	return all((l[i] >= l[i + 1] for i in range(len(l) - 1)))
q = int(input())
for _ in range(q):
	n = int(input())
	v = list(map(int, input().split()))
	possible = False
	index = v.index(1)
	left = v[:index]
	right = v[index:]
	att1 = right + left
	index = v.index(n)
	left = v[:index]
	right = v[index:]
	att2 = right + left
	possible = is_r_sorted(att2) or is_sorted(att1)
	if possible:
		print('YES')
	else:
		print('NO')


q = int(input())
answer = []
for i in range(q):
	ans = 'YES'
	n = int(input())
	lis = list(map(int, input().split(' ')))
	if n > 1:
		if lis[0] < lis[1] and lis[1] == lis[0] + 1 or (lis[0] == n and lis[1] == 1):
			for i in range(1, n):
				if lis[i - 1] != n:
					if lis[i] == lis[i - 1] + 1:
						continue
					else:
						ans = 'NO'
						break
				elif lis[i - 1] + 1 == 1:
					continue
		else:
			for i in range(1, n):
				if lis[i - 1] != 1:
					if lis[i] == lis[i - 1] - 1:
						continue
					else:
						ans = 'NO'
						break
				elif lis[i - 1] + 1 == n:
					continue
	answer.append(ans)
for i in range(q):
	print(answer[i])


from collections import deque

def check_clock(a, n):
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			return False
	return True

def check_anti(a, n):
	for i in range(n - 1):
		if a[i] < a[i + 1]:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	a = deque(list(map(int, input().split())))
	flag = False
	for i in range(n):
		a.rotate(1)
		if check_clock(a, n) or check_anti(a, n):
			flag = True
			break
	for i in range(n):
		a.rotate(1)
		if check_clock(a, n) or check_anti(a, n):
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


p = int(input())
M = []
for i in range(p):
	n = int(input())
	L = [int(i) for i in input().split()]
	M.append(L)
for i in range(p):
	T = True
	L = M[i]
	for i in range(len(L) - 1):
		if L[i] == len(L):
			if i == 0:
				if L[i + 1] not in {1, len(L) - 1}:
					print('No')
					T = False
					break
			elif L[i + 1] not in {1, len(L) - 1} or L[i - 1] not in {1, len(L) - 1}:
				print('No')
				T = False
				break
		elif L[i] - L[i + 1] != 1 and L[i] - L[i + 1] != -1 and (L[i] != len(L)) and (L[i + 1] != len(L)):
			print('No')
			T = False
			break
	if T == True:
		print('Yes')


q = int(input())
for i in range(q):
	n = int(input())
	arr = [int(x) for x in input().split()]
	l = arr[0]
	forwards = True
	back = True
	for j in range(n - 1):
		if arr[j] - 1 != (arr[0] - 1 + j) % n:
			forwards = False
		if arr[j] - 1 != (arr[0] - 1 - j) % n:
			back = False
	if forwards == True or back == True:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = [int(item) for item in input().split()]
	prev = a[-1]
	is_counter = True
	for item in a:
		if prev == n:
			if item != 1:
				is_counter = False
				break
		elif item != prev + 1:
			is_counter = False
			break
		prev = item
	if is_counter:
		print('YES')
		continue
	prev = a[0]
	is_ccounter = True
	for item in a[::-1]:
		if prev == n:
			if item != 1:
				is_ccounter = False
				break
		elif item != prev + 1:
			is_ccounter = False
			break
		prev = item
	if is_ccounter:
		print('YES')
		continue
	print('NO')


n = int(input())
for i in range(n):
	m = int(input())
	l = [int(x) for x in input().split()][0:m]
	i = l.index(1)
	l = l[i:] + l[:i]
	res = True
	for i in range(m):
		if i != l[i] - 1:
			res = False
			break
	if not res:
		i = l.index(m)
		l = l[i:] + l[:i]
		res = True
		for i in range(m):
			if i != l[m - i - 1] - 1:
				res = False
				break
	if res:
		print('YES')
	else:
		print('NO')


from math import gcd
import sys

def inpu():
	return sys.stdin.readline().strip()
q = int(inpu())
for i in range(q):
	n = int(inpu())
	mas = [int(i) for i in inpu().split()]
	mi = mas.index(min(mas))
	no = False
	j = mi
	c = True
	for i in range(len(mas) - 1):
		if mas[i + 1] - mas[i] != 1 and mas[i + 1] != 1:
			no = True
			break
	if not no:
		print('YES')
		continue
	ma = mas.index(max(mas))
	no = False
	j = ma
	c = True
	for i in range(len(mas) - 1):
		if mas[i] - mas[i + 1] != 1 and mas[i + 1] != len(mas):
			no = True
			break
	if not no:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
input = stdin.readline

def clockwise(a, b, n):
	return b == a + 1 or (a == n and b == 1)

def counterclockwise(a, b, n):
	return b == a - 1 or (a == 1 and b == n)
q = int(input())
for query in range(q):
	n = int(input())
	p = [int(i) for i in input().split()]
	p.append(p[0])
	(ok1, ok2) = (True, True)
	for i in range(n):
		ok1 &= clockwise(p[i], p[i + 1], n)
		ok2 &= counterclockwise(p[i], p[i + 1], n)
	if ok1 or ok2:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(1, q + 1):
	n = int(input())
	D = list(map(int, input().split()))
	a = D.index(1)
	Q = D[a:] + D[0:a]
	if Q == [k for k in range(1, n + 1)] or Q == [1] + [k for k in range(n, 1, -1)]:
		print('YES')
	else:
		print('NO')


q = int(input())
ans = ['YES'] * q
for i in range(q):
	n = int(input())
	p = list(map(int, input().split(' ')))
	mxp = max(p)
	mnp = min(p)
	a = True
	b = True
	for j in range(n):
		if (p[j] >= p[(j + 1) % n] or (p[j] == mnp and p[(j + 1) % n] == mxp)) and (p[j] <= p[j - 1] or (p[j] == mxp and p[j - 1] == mnp)):
			continue
		else:
			a = False
	for j in range(n):
		if (p[j] <= p[(j + 1) % n] or (p[j] == mxp and p[(j + 1) % n] == mnp)) and (p[j] >= p[j - 1] or (p[j] == mnp and p[j - 1] == mxp)):
			continue
		else:
			b = False
	if not (a or b):
		ans[i] = 'NO'
for i in range(q):
	print(ans[i])


for _ in range(0, int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	idx = -1
	for i in range(0, n):
		if a[i] == 1:
			idx = i
	assert idx >= 0
	v1 = True
	v2 = True
	for i in range(0, n):
		v1 = v1 and a[(i + idx + n) % n] == i + 1
		v2 = v2 and a[(i - (n - 1 - idx) + n) % n] == n - i
	if v1 or v2:
		print('YES')
	else:
		print('NO')


import sys
import os
from io import IOBase, BytesIO
import math
import collections
mod = 10 ** 9 + 7
pie = 3.1415926536

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, p):
	t = fact[n] * (ifact[r] * ifact[n - r] % p) % p
	return t

def ain():
	return list(map(int, sin().split()))

def sin():
	return input().strip()

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def read2DIntArray(row, col):
	arr = []
	for i in range(0, row):
		temp = list(map(int, sin().split()))
		arr.append(temp)
	return arr

def read2DCharArray(row, col):
	arr = []
	for i in range(0, row):
		temp = str(sin())
		arr.append(temp)
	return arr

def smallestNumber(n):
	lst = list(str(n))
	lst.sort()
	tmp = ''
	for (i, n) in enumerate(lst):
		if n != '0':
			tmp = lst.pop(i)
			break
	return str(tmp) + ''.join(lst)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	lst = []
	for p in range(n + 1):
		if prime[p]:
			lst.append(p)
	return lst

def nCr(n, r):
	a = 1
	b = 1
	c = 1
	for i in range(1, n + 1):
		c *= i
	for i in range(1, r + 1):
		b *= i
	for i in range(1, n - r + 1):
		a *= i
	return c // (a * b)

def sieveOfEratosthenes1(N, s):
	prime = [False] * (N + 1)
	for i in range(2, N + 1, 2):
		s[i] = 2
	for i in range(3, N + 1, 2):
		if prime[i] == False:
			s[i] = i
			for j in range(i, int(N / i) + 1, 2):
				if prime[i * j] == False:
					prime[i * j] = True
					s[i * j] = i

def generatePrimeFactors(N):
	s = [0] * (N + 1)
	sieveOfEratosthenes1(N, s)
	curr = s[N]
	cnt = 1
	factors = []
	power = []
	while N > 1:
		N //= s[N]
		if curr == s[N]:
			cnt += 1
			continue
		factors.append(curr)
		power.append(cnt)
		curr = s[N]
		cnt = 1
	return (factors, power)

def main():
	for _ in range(int(sin())):
		n = int(sin())
		a = ain()
		tmp = []
		for i in range(n):
			tmp.append(abs(a[i % n] - a[(i + 1) % n]))
		c = tmp.count(1)
		if c >= n - 1:
			print('YES')
		else:
			print('NO')
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


q = int(input())
for _ in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	p = 0
	for i in range(n):
		if a[i] == 1:
			p = i
			break
	cnt = 0
	pnext = p
	direction = False
	f = 0
	for __ in range(n):
		pnext = (p + 1) % n
		if direction == False:
			if a[pnext] == 2:
				f = 1
			elif a[pnext] == n:
				f = -1
			else:
				break
			direction = True
			cnt += 1
		elif a[p] == n and a[pnext] == 1 and (f == 1) or (a[p] == 1 and a[pnext] == n and (f == -1)) or a[pnext] - a[p] == f:
			cnt += 1
		p = pnext
	if cnt == n:
		print('YES')
	else:
		print('NO')


q = int(input())
while q > 0:
	n = int(input())
	p = [int(ele) for ele in input().split()]
	count1 = 0
	count2 = 0
	maxi = max(p)
	mini = min(p)
	max_i = p.index(maxi)
	min_i = p.index(mini)
	j = max_i
	while count1 < n:
		if j == n - 1:
			i = 0
			if p[i] == p[j] - 1:
				count1 += 1
				j = i
				i += 1
			else:
				break
		else:
			i = j + 1
			if p[i] == p[j] - 1:
				count1 += 1
				j = i
				i += 1
			else:
				break
	j = min_i
	while count2 < n:
		if j == n - 1:
			i = 0
			if p[i] == p[j] + 1:
				count2 += 1
				j = i
			else:
				break
		else:
			i = j + 1
			if p[i] == p[j] + 1:
				count2 += 1
				j = i
			else:
				break
	if count1 == n - 1:
		print('YES')
	elif count2 == n - 1:
		print('YES')
	else:
		print('NO')
	q -= 1


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def int1():
	return int(input())
import string
import math
from itertools import combinations

def main():
	for q in range(int(input())):
		n = int1()
		a = get_array()
		m = max(a)
		i = 0
		z = 0
		while i < n - 1:
			if a[i + 1] - a[i] == 1:
				z = z + 1
			elif a[i + 1] == 1:
				z = z + 1
			else:
				break
			i = i + 1
		if z == n - 1 or n == 1:
			print('YES')
		else:
			j = 0
			r = 0
			while j < n - 1:
				if a[j + 1] == m:
					r = r + 1
				elif a[j] - a[j + 1] == 1:
					r = r + 1
				else:
					break
				j = j + 1
			if r == n - 1:
				print('YES')
			else:
				print('NO')
		n = n - 1
main()


q = int(input())
for _ in range(q):
	n = int(input())
	A = list(map(int, input().split()))
	p = A.index(1)
	c1 = 1
	c2 = 1
	for i in range(1, n):
		if A[(p - i + n) % n] != i + 1:
			c1 = 0
		if A[(p + i + n) % n] != i + 1:
			c2 = 0
	if c1 == 1 or c2 == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	p = [0 for i in range(n)]
	for i in range(n):
		p[l[i] - 1] = i + 1
	flag = True
	cnt = 0
	while flag:
		up = p == sorted(p)
		down = p[::-1] == sorted(p[::-1])
		p = [p[n - 1]] + p[:n - 1]
		if up or down:
			flag = False
			print('YES')
		if cnt == n:
			flag = False
			print('NO')
		cnt += 1


from collections import deque
I = lambda : map(int, input().split())
for i in range(int(input())):
	res = 0
	n = int(input())
	a = list(I())
	a = deque(a)
	a.append(a[0])
	a.appendleft(a[-2])
	for j in range(1, n):
		if a[j] == n:
			e2 = 1
		else:
			e2 = a[j] + 1
		if a[j] == 1:
			e1 = n
		else:
			e1 = a[j] - 1
		if a[j - 1] in [e1, e2] and a[j + 1] in [e1, e2]:
			pass
		else:
			res = 1
			break
	print('YNEOS'[res::2])


for _ in range(int(input())):
	n = int(input())
	p = [*map(int, input().split())]
	ind = 0
	for i in range(n):
		if p[i] == 1:
			ind = i
			break
	l = [ind for ind in range(1, n + 1)]
	l1 = p[ind:] + p[:ind]
	l2 = [*reversed(p[:ind + 1])] + ([*reversed(p[ind + 1:])] if ind < n - 1 else [])
	if l1 == l or l2 == l:
		print('YES')
	else:
		print('NO')


from __future__ import division, print_function
import os
import sys
from difflib import SequenceMatcher
from io import BytesIO, IOBase
from collections import Counter, deque
from heapq import heapify, heappush, heappop
from math import gcd, ceil, sqrt, log2, log
from itertools import accumulate, repeat, permutations, compress
import operator
from operator import itemgetter
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def rl():
	return list(map(int, input().split()))

def arp(arr):
	return ' '.join([str(i) for i in arr])
for _ in range(int(input())):
	(n,) = rl()
	arr = rl()
	f = 0
	for i in range(n - 1):
		if abs(arr[i] - arr[i + 1]) == 1:
			continue
		elif abs(arr[i] - arr[i + 1]) != n - 1:
			f += 2
		else:
			f += 1
		if f > 1:
			break
	if f <= 1:
		print('YES')
	else:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')


def check(n, ar):
	if n == 1:
		return 'YES'
	route = (ar[1] - ar[0]) % n
	if route != 1 and route != n - 1:
		return 'NO'
	for i in range(0, n):
		if (ar[i] - ar[(i - 1) % n]) % n != route:
			return 'NO'
	return 'YES'
q = int(input().strip())
while q:
	q -= 1
	n = int(input().strip())
	p = list(map(int, input().rstrip().split()))
	print(check(n, p))


q = int(input())
r = []
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	diff = [abs(l[i + 1] - l[i]) for i in range(len(l) - 1)]
	diff.append(abs(l[0] - l[-1]))
	if diff.count(1) >= len(diff) - 1:
		r.append('YES')
	else:
		r.append('NO')
[print(i) for i in r]


def increasing(arr):
	n = len(arr)
	for i in range(n - 1):
		if arr[i + 1] - arr[i] != 1:
			return False
	return True

def clock(arr, n):
	index = arr.index(1)
	arr1 = []
	for i in range(index, n):
		arr1.append(arr[i])
	for i in range(index):
		arr1.append(arr[i])
	if increasing(arr1):
		return True
	return False

def anticlock(arr, n):
	index = arr.index(1)
	arr1 = []
	for i in range(index, -1, -1):
		arr1.append(arr[i])
	for i in range(n - 1, index, -1):
		arr1.append(arr[i])
	if increasing(arr1):
		return True
	return False

def solve(n, arr):
	if n == 1:
		print('YES')
		return
	if clock(arr, n) or anticlock(arr, n):
		print('YES')
		return
	print('NO')

def main():
	q = int(input())
	for i in range(q):
		n = int(input())
		arr = list(map(int, input().split()))
		solve(n, arr)
main()


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	c = 0
	for j in range(0, len(l) - 1):
		if abs(l[j] - l[j + 1]) == 1 or abs(l[j] - l[j + 1]) == n - 1:
			count += 1
		else:
			c += 1
	if c == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	s.append(s[0])
	a = set()
	for i in range(n):
		a.add(s[i + 1] - s[i])
	if len(a) < 3:
		if a == {0}:
			print('YES')
		elif -1 in a and n - 1 in a or (1 in a and -n + 1 in a):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = []
	c = 0
	d = 0
	for i in range(n - 1):
		if l[i + 1] - l[i] == 1 or l[i + 1] - l[i] == 1 - n:
			c = c + 1
		if l[i + 1] - l[i] == -1 or l[i + 1] - l[i] == n - 1:
			d = d + 1
	if c == n - 1 or d == n - 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = f = i = 0
	while i < n:
		if abs(l[i] - l[i - 1]) == 1 or abs(l[i] - l[i - 1]) == max(l) - 1:
			p += 1
		i = i + 1
	print('YES' if p == n else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	if a == sorted(a) or a == sorted(a, reverse=True):
		print('YES')
	else:
		i = 0
		c = 0
		while i < n:
			if abs(a[i] - a[i - 1]) > 1:
				c += 1
			i += 1
		if c < 2:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1 or n == 2 or n == 3:
		print('YES')
	else:
		s = ''
		for i in arr:
			s = s + str(i) + ' '
		s = s * 2
		s1 = ''
		s2 = ''
		for i in range(1, n + 1):
			s1 = s1 + str(i) + ' '
			s2 = ' ' + str(i) + s2
		if s1 in s or s2 in s:
			print('YES')
		else:
			print('NO')


q = int(input().strip())
for _ in range(q):
	n = int(input().strip())
	x = list(map(int, input().strip().split()))
	if x[x.index(1):] + x[:x.index(1)] == list(range(1, n + 1)) or x[x.index(n):] + x[:x.index(n)] == list(range(1, n + 1))[::-1]:
		print('YES')
	else:
		print('NO')


I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	n = I()
	a = LI()
	flag = 1
	for i in range(n - 1):
		if abs(a[i] - a[i + 1]) == 1:
			continue
		elif abs(a[i] - a[i + 1]) == n - 1:
			continue
		else:
			flag = 0
			print('NO')
			break
	if flag:
		print('YES')


a = int(input())
for i in range(a):
	status = True
	m = int(input())
	ll = [int(x) for x in input().split()]
	g = sorted(ll)
	if ll == g:
		print('YES')
	elif ll[::-1] == g:
		print('YES')
	else:
		for j in range(1, m):
			iop = int(ll[j - 1]) - int(ll[j])
			if iop == 1:
				pass
			elif iop == -1:
				pass
			elif iop == 1 - m:
				pass
			elif iop == m - 1:
				pass
			else:
				status = False
			if status is False:
				print('NO')
				break
		if status is True:
			print('YES')
	ll.clear()
	g = 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 3:
		print('YES')
		continue
	start = a[0]
	b1 = list(range(start, n + 1)) + list(range(1, start))
	b2 = list(range(1, start + 1))[::-1] + list(range(start + 1, n + 1))[::-1]
	if a == b1 or a == b2:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	i1 = arr.index(1)
	flg = 0
	cwise = arr[i1:] + arr[:i1]
	ccwise = list(reversed(arr[i1 + 1:] + arr[:i1 + 1]))
	s_arr = sorted(arr)
	if cwise == s_arr or ccwise == s_arr:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	t = sorted(a)[:]
	t1 = t[::-1]
	if len(a) > 1:
		f = True
		for i in range(n):
			if t == a or t1 == a:
				f = False
				print('YES')
				break
			a = [a[-1]] + a[:-1]
		if f:
			print('NO')
	else:
		print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	if n == 1:
		print('YES')
	else:
		clockwise = True
		counter = True
		if p[0] == n:
			compare = 1
		else:
			compare = p[0] + 1
		for i in range(1, n):
			if p[i] != compare:
				clockwise = False
				break
			if compare == n:
				compare = 1
			else:
				compare += 1
		if p[n - 1] == n:
			compare = 1
		else:
			compare = p[n - 1] + 1
		for i in range(n - 2, -1, -1):
			if p[i] != compare:
				counter = False
				break
			if compare == n:
				compare = 1
			else:
				compare += 1
		if clockwise or counter:
			print('YES')
		else:
			print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	ar = list(map(int, input().split()))
	c1 = 0
	c2 = 0
	for i in range(n):
		if ar[i] == ar[i - 1] + 1:
			c1 += 1
		elif ar[i] == ar[i - 1] - 1:
			c2 += 1
	if c1 == n - 1 or c2 == n - 1:
		print('YES')
	else:
		print('NO')


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
for _ in range(int(input())):
	n = int(input())
	a = listin()
	z = a + a + a
	start = []
	for i in range(len(z)):
		if z[i] == 1:
			start.append(i)
	start = start[1]
	arr1 = []
	arr2 = []
	for i in range(n):
		arr1.append(z[start + i])
		arr2.append(z[start - i])
	key = [i for i in range(1, n + 1)]
	if key == arr1 or arr2 == key:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	ind = [int(i) for i in input().split(' ')]
	s = ind.index(1)
	ind = ind[s:] + ind[0:s]
	if ind == list(range(1, n + 1)):
		print('YES')
	else:
		ind = ind[1:] + ind[0:1]
		if ind == list(range(n, 0, -1)):
			print('YES')
		else:
			print('NO')


def main():
	n = input()
	p = list(map(int, input().split()))
	isFirst = True
	isSecond = True
	while p[0] != 1:
		x = p[0]
		for i in range(len(p) - 1):
			p[i] = p[i + 1]
		p[-1] = x
	for i in range(len(p) - 1):
		if p[i] + 1 != p[i + 1]:
			isFirst = False
	x = p[0]
	for i in range(len(p) - 1):
		p[i] = p[i + 1]
	p[-1] = x
	for i in range(len(p) - 1):
		if p[i] - 1 != p[i + 1]:
			isSecond = False
	if isFirst or isSecond:
		print('YES')
	else:
		print('NO')
q = int(input())
for i in range(q):
	main()


import sys
from collections import deque
n = int(next(sys.stdin))
for _ in range(n):
	l = int(next(sys.stdin))
	students = deque(map(int, next(sys.stdin).split(' ')))
	while students[l - 1] != 1:
		students.rotate(1)
	if list(students) == sorted(students, reverse=True):
		print('YES')
		continue
	while students[0] != 1:
		students.rotate(1)
	if list(students) == sorted(students):
		print('YES')
		continue
	print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	List = list(map(int, input().split()))
	n -= 1
	s = 0
	for i in range(n):
		s = abs(List[i] - List[i + 1])
		if s == 1 or s == n:
			continue
		s = -1
		break
	if s == -1:
		print('NO')
	else:
		print('YES')


t = int(input())
for r in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ind = a.index(1)
	i = 0
	arr = []
	arr.append(1)
	ind = ind + 1
	while a[ind % n] != 1:
		arr.append(a[ind % n])
		ind = ind + 1
	ind = a.index(1)
	arr2 = []
	i = 0
	arr2.append(1)
	ind = ind - 1
	while a[ind % n] != 1:
		arr2.append(a[ind % n])
		ind = ind - 1
	a.sort()
	if arr == a or arr2 == a:
		print('YES')
	else:
		print('NO')


n = int(input())
i = 0
flag = False
while i < n:
	m = int(input())
	if m == 1:
		g = list(map(int, input().split()))
		print('YES')
		i += 1
		continue
	a = list(map(int, input().split()))
	j = 0
	while a[j] != 1:
		j += 1
	b = []
	for k in range(m - j):
		b.append(a[j + k])
	for k in range(j):
		b.append(a[k])
	for j in range(m - 1):
		if b[j] == j + 1:
			flag = True
		else:
			flag = False
			break
	if flag == True:
		i += 1
		print('YES')
		continue
	else:
		j = 0
		while a[j] != m:
			j += 1
		b = []
		for k in range(m - j):
			b.append(a[j + k])
		for k in range(j):
			b.append(a[k])
		for j in range(m):
			if b[j] == m - j:
				flag = True
			else:
				flag = False
				break
		if flag:
			print('YES')
			i += 1
			continue
		else:
			print('NO')
			i += 1
			continue


nn = int(input())

def make_try(number_list):
	b = False
	index = number_list.index(1)
	index_o = index
	sch = 0
	sch_o = 0
	for i in range(len(number_list)):
		if number_list[(index + 1) % len(number_list)] - number_list[index % len(number_list)] == 1 or (i == len(number_list) - 1) & (number_list[(index + 1) % len(number_list)] == 1):
			sch += 1
		if number_list[index_o - 1] - number_list[index_o] == 1 or (i == len(number_list) - 1) & (number_list[index_o - 1] == 1):
			sch_o += 1
		index += 1
		index_o -= 1
	if sch == len(number_list) or sch_o == len(number_list):
		b = True
	return b
for i in range(nn):
	b = False
	n = int(input())
	number_list_n = input().split(' ')
	number_list = [int(item) for item in number_list_n]
	if make_try(number_list):
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	flag = True
	for i in range(1, n - 1):
		if p[i] != p[i - 1] + 1 and p[i] % n != p[i - 1] % n + 1:
			flag = False
	flag2 = True
	for i in range(1, n - 1):
		if p[i] != p[i - 1] - 1 and p[i] % n != p[i - 1] % n - 1:
			flag2 = False
	if flag or flag2:
		print('YES')
	else:
		print('NO')


count = int(input())
result = []
for _ in range(count):
	_ = input()
	circle = list(map(lambda num: int(num), input().split()))
	goodc = circle.copy()
	goodc.sort()
	flag = 1
	for i in range(len(circle)):
		test = circle[i:] + circle[:i]
		if test == goodc or test == goodc[::-1]:
			result.append('YES')
			flag = 0
			break
	if flag:
		result.append('NO')
print('\n'.join(result))


n = int(input())
while n > 0:
	n -= 1
	x = int(input())
	l = list(map(int, input().split()))
	l = l + l
	l1 = list(reversed(l))
	l2 = l
	i1 = l1.index(1)
	i2 = l2.index(1)
	flag = 0
	for i in range(1, x + 1):
		if l1[i1] != i:
			flag = 1
		i1 += 1
	if flag == 0:
		print('YES')
		continue
	flag = 0
	for i in range(1, x + 1):
		if l2[i2] != i:
			flag = 1
		i2 += 1
	if flag == 1:
		print('NO')
	else:
		print('YES')


import atexit, io, sys
input_lines = sys.stdin.read().splitlines()
input = iter(input_lines).__next__
out_buffer = io.StringIO()
sys.stdout = out_buffer

@atexit.register
def write():
	sys.__stdout__.write(out_buffer.getvalue())

def line():
	return map(int, input().split())
from itertools import repeat, tee
from operator import eq

def pairwise(it):
	(a, b) = tee(it)
	next(b)
	return zip(a, b)
q = int(input())
for _ in repeat(None, q):
	n = int(input())
	p = list(line())
	if len(p) == 1:
		print('YES')
		continue
	w = p.index(1)
	if p[(w + 1) % n] == 2:
		p = p[w:] + p[:w]
	else:
		p = p[w::-1] + p[:w:-1]
	if all(map(eq, p, range(1, n + 1))):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 'YES'
	l = [i for i in range(1, n + 1)]
	for i in range(n):
		if a[i] == 1:
			break
	if all((a[(i + k) % n] == l[k] for k in range(n))) or all((a[(i - k) % n] == l[k] for k in range(n))):
		pass
	else:
		ans = 'NO'
	print(ans)


q = int(input())
for test in range(q):
	n = int(input())
	arr = [int(x) - 1 for x in input().split()]
	x = arr.index(min(arr))
	i = 0
	flag = 0
	while i < n:
		if x == n:
			x = 0
		if arr[x] == i:
			i += 1
			x += 1
		else:
			flag += 1
			break
	i = 0
	x = arr.index(min(arr))
	while i < n:
		if x == -1:
			x = n - 1
		if arr[x] == i:
			i += 1
			x -= 1
		else:
			flag += 1
			break
	if flag == 2:
		print('NO')
	else:
		print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	flag = 0
	if n == 1:
		flag = 1
	else:
		for j in range(n - 1):
			if abs(p[j] - p[j + 1]) == 1 or abs(p[j] - p[j + 1]) == n - 1:
				flag = 1
			else:
				flag = 0
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	positions = input().split()
	k = 0
	for j in range(n):
		positions[j] = int(positions[j])
		if positions[j] == 1:
			k = j
	if_hour = True
	if_reverse = True
	for l in range(k - 1, k - n, -1):
		if positions[l] - positions[l - 1] != 1:
			if_reverse = False
			break
	if not if_reverse:
		for l in range(k, k - n + 1, -1):
			if positions[l] - positions[l - 1] != -1:
				if_hour = False
				break
	if if_hour or if_reverse:
		print('YES')
	else:
		print('NO')


q = int(input())
for j in range(q):
	n = int(input())
	data = list(map(int, input().split()))
	forward = True
	backward = True
	if n == 1:
		print('YES')
	else:
		for i in range(1, n):
			if data[i] == data[i - 1] + 1:
				forward = True
			elif data[i - 1] == n and data[i] == 1:
				forward = True
			else:
				forward = False
				break
		data = data[::-1]
		for i in range(1, n):
			if data[i] == data[i - 1] + 1:
				backward = True
			elif data[i - 1] == n and data[i] == 1:
				backward = True
			else:
				backward = False
				break
		if backward or forward:
			print('YES')
		else:
			print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(p) for p in input().split()]
	ind = arr.index(1)
	f = ind + 1
	b = ind - 1
	fs = [1]
	bs = [1]
	if f == len(arr):
		f = 0
	while arr[f] != 1:
		if f == len(arr) - 1:
			fs.append(arr[f])
			f = 0
			continue
		fs.append(arr[f])
		f += 1
	while arr[b] != 1:
		bs.append(arr[b])
		b -= 1
	if sorted(bs) == bs or sorted(fs) == fs:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	f = True
	for i in range(1, n):
		if l[i] != 1:
			if l[i] - l[i - 1] != 1:
				f = False
				break
	if f:
		print('YES')
	else:
		f = True
		for i in range(1, n):
			if l[i - 1] != 1:
				if l[i] - l[i - 1] != -1:
					f = False
					break
		if f:
			print('YES')
		else:
			print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	array = list(map(int, input().split()))
	if len(array) < 3:
		print('YES')
	else:
		first = array.index(1)
		flag = 0
		for i in range(n - 1):
			if abs(array[i] - array[i + 1]) > 1:
				flag += 1
		if abs(array[0] - array[n - 1]) > 1:
			flag += 1
		print('YES' if flag == 1 else 'NO')


q = int(input())
result = []
for _ in range(q):
	input()
	students = list(map(int, input().split()))
	direction = 0
	prev = None
	changed = False
	c_pos = None
	r = 'YES'
	for (i, s) in enumerate(students):
		if prev:
			if direction == 0:
				if s == prev + 1:
					direction = 1
				elif s == prev - 1:
					direction = -1
				else:
					changed = True
					if prev < s:
						direction = -1
					else:
						direction = 1
			elif s != prev + direction:
				if changed:
					r = 'NO'
				else:
					changed = True
		prev = s
	result.append(r)
print('\n'.join(result))


from sys import stdin, stdout
import bisect
import math

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def soe(limit):
	l = [1] * (limit + 1)
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def solve():
	n = inp()
	l = li()
	x = l[0]
	i = 1
	(c, d) = (0, 0)
	while i < n:
		if l[i] != x % n + 1:
			c = 1
			break
		else:
			x = l[i]
		i += 1
	j = n - 1
	x = l[0]
	while j > 0:
		if l[j] != x % n + 1:
			d = 1
			break
		else:
			x = l[j]
		j -= 1
	if c == 0 or d == 0:
		print('YES')
	else:
		print('NO')
for _ in range(inp()):
	solve()


q = int(input())
while q > 0:
	q -= 1
	n = int(input())
	p = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		index = p.index(1)
		i = index
		j = (index + 1) % n
		flag1 = True
		while j != index:
			if p[i] > p[j]:
				flag1 = False
				break
			i = (i + 1) % n
			j = (j + 1) % n
		flag2 = True
		i = index
		j = index - 1
		if j == -1:
			j = n - 1
		while j != index:
			if p[i] > p[j]:
				flag2 = False
				break
			i = i - 1
			j = j - 1
			if i == -1:
				i = n - 1
			if j == -1:
				j = n - 1
		if flag1 or flag2:
			print('YES')
		else:
			print('NO')


from sys import stdin
q = int(stdin.readline())
dance = 'YES'
for i in range(0, q):
	n = int(stdin.readline())
	a = list((int(i) for i in stdin.readline().split()))
	countplus = 0
	countminus = 0
	for j in range(0, n - 1):
		if a[j] - a[j + 1] == 1:
			countplus += 1
		if a[j] - a[j + 1] == -1:
			countminus += 1
	if a[n - 1] - a[0] == 1:
		countplus += 1
	if a[n - 1] - a[0] == -1:
		countminus += 1
	if countplus == n - 1 or countminus == n - 1:
		print('YES')
	else:
		print('NO')


n = int(input())
ans = []
for i in range(n):
	k = int(input())
	l = list(map(int, input().split()))
	w = 0
	for i in range(len(l) - 1):
		if l[i + 1] - l[i] == 1 or l[i + 1] - l[i] == k - 1 or l[i + 1] - l[i] == -1 or (l[i + 1] - l[i] == -k + 1):
			pass
		else:
			w += 1
			break
	if w != 0:
		ans.append('NO')
	else:
		ans.append('YES')
print('\n'.join(ans))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	stu = list(map(int, input().split()))
	pos = stu.index(1)
	lft = 1
	rght = 1
	for j in range(1, n):
		lft &= stu[(n + pos - j) % n] == j + 1
		rght &= stu[(n + pos + j) % n] == j + 1
	if lft or rght:
		print('YES')
	else:
		print('NO')


array = []
x = int(input())
for i in range(x):
	n = int(input())
	array = [int(x) for x in input().split()]
	j = 0
	for j in range(n):
		if 1 == array[j]:
			minIndex = j
			break
	m = 0
	l = 0
	if array[(minIndex + 1) % n] == 2:
		if n == 2:
			print('YES')
		while m < n - 2:
			if array[(minIndex + 1 + m) % n] + 1 != array[(minIndex + 2 + m) % n]:
				print('NO')
				break
			elif m == n - 3:
				print('YES')
				m += 1
			else:
				m += 1
	elif array[minIndex - 1] == 2:
		while m < n - 2:
			if array[(minIndex - m - 1) % n] + 1 != array[(minIndex - 2 - m) % n]:
				print('NO')
				break
			elif m == n - 3:
				print('YES')
				m += 1
			else:
				m += 1
	elif n == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	for i in range(n):
		if lis == sorted(lis) or lis == sorted(lis, reverse=True):
			print('YES')
			break
		lis.insert(0, lis.pop())
	else:
		print('NO')


cc = int(input())
while cc > 0:
	cc -= 1
	ll = int(input())
	(*aa,) = map(int, input().split())
	i = aa.index(1)
	inc = [1, -1][aa[i - 1] == 2]
	pre = 1
	ans = 1
	i += inc
	if i == ll:
		i = 0
	while aa[i] != 1:
		if abs(aa[i] - pre) != 1:
			ans = 0
			break
		pre = aa[i]
		i += inc
		if i == ll:
			i = 0
	print(['NO', 'YES'][ans])


for i in range(int(input())):
	n = int(input())
	x = input().strip()
	x += ' ' + str(x)
	y = ' '.join((str(i) for i in range(1, n + 1)))
	z = ' '.join([str(i) for i in range(1, n + 1)][::-1])
	if y in x or z in x:
		print('YES')
	else:
		print('NO')


casos = int(input())
for i in range(casos):
	numero = int(input())
	lista = [int(x) for x in input().split()]
	contador = 0
	for i in range(numero - 1):
		if abs(lista[i] - lista[i + 1]) != 1:
			contador += 1
	if contador > 1:
		print('NO')
		continue
	contadorHorario = 0
	contadorAntihorario = 0
	for i in range(numero):
		if lista[i % numero] > lista[(i + 1) % numero]:
			contadorHorario += 1
		if lista[i % numero] < lista[(i + 1) % numero]:
			contadorAntihorario += 1
	if contadorAntihorario <= 1 or contadorHorario <= 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	P = list(map(int, input().split()))
	if n == 1 or n == 2:
		print('YES')
	else:
		c = (P[1] - P[0]) % n
		if c not in [1, n - 1]:
			print('NO')
		else:
			for i in range(2, n):
				if (P[i] - P[i - 1]) % n != c:
					print('NO')
					c = 0
					break
			if c != 0:
				print('YES')


a = int(input())
arg = []
for j in range(a):
	prov1 = 1
	prov2 = 1
	z = int(input())
	x = [int(i) for i in input().split()]
	y = min(x)
	p = x.index(y)
	for i in range(p, len(x) + p):
		arg.append(x[i % len(x)])
	for i in range(1, len(arg)):
		if arg[i] - arg[i - 1] != 1:
			prov1 = 0
			break
	arg = []
	y = max(x)
	p = x.index(y)
	for i in range(p, len(x) + p):
		arg.append(x[i % len(x)])
	for i in range(1, len(arg)):
		if arg[i] - arg[i - 1] != -1:
			prov2 = 0
			break
	prov = prov1 or prov2
	if prov:
		print('YES')
	else:
		print('NO')
	arg = []


from collections import deque
import sys

def inp():
	return sys.stdin.readline().strip()
for _ in range(int(inp())):
	n = int(inp())
	a = list(map(int, inp().split()))
	p = a.index(1)
	ct = 0
	while ct < n:
		if a[p] != ct + 1:
			break
		ct += 1
		p = (p + 1) % n
	if ct == n:
		print('YES')
		continue
	p = a.index(1)
	ct = 0
	while ct < n:
		if a[p] != ct + 1:
			break
		ct += 1
		p = (p - 1) % n
	if ct == n:
		print('YES')
		continue
	print('NO')


import sys
input = sys.stdin.readline

def getInt():
	return int(input())

def getVars():
	return map(int, input().split())

def getArr():
	return list(map(int, input().split()))

def getStr():
	return input().strip()
Q = getInt()
for _ in range(Q):
	N = getInt()
	a = getArr()
	yes1 = True
	yes2 = True
	m = max(a)
	for i in range(1, N):
		if not (a[i] == a[i - 1] + 1 or (a[i] == 1 and a[i - 1] == m)):
			yes1 = False
			break
	for i in range(1, N):
		if not (a[i] + 1 == a[i - 1] or (a[i] == m and a[i - 1] == 1)):
			yes2 = False
			break
	if yes1 or yes2:
		print('YES')
	else:
		print('NO')


for tc in range(int(input())):
	n = int(input())
	arr = [*map(int, input().split())] * 3
	idx = arr.index(1)
	clock = True
	anti = True
	for i in range(n):
		clock &= arr[idx + i] == i + 1
	idx = len(arr) - 1 - arr[::-1].index(1)
	for i in range(n):
		anti &= arr[idx - i] == i + 1
	print('YES' if clock or anti else 'NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	offset = p.index(1)
	if offset == -1:
		print('NO')
		continue
	success1 = True
	success2 = True
	for j in range(n - 1):
		if p[(offset + j) % n] + 1 != p[(offset + j + 1) % n]:
			success1 = False
		if p[(offset - j) % n] + 1 != p[(offset - j - 1) % n]:
			success2 = False
	if success1 or success2:
		print('YES')
	else:
		print('NO')


def circle(arr, idx, n):
	idx = arr.index(1)
	(right, left) = (1, 1)
	for i in range(n):
		right &= arr[(idx - i + n) % n] == i + 1
		left &= arr[(idx + i + n) % n] == i + 1
	if right or left == 1:
		return 'YES'
	return 'NO'
t = int(input())
while t:
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	idx = arr.index(1)
	print(circle(arr, idx, n))
	t = t - 1


n = int(input())
for i in range(n):
	a = int(input())
	if a == 1:
		t = True
	sata = list(map(int, input().split()))
	for j in range(len(sata) - 1):
		if max(sata[j], sata[j + 1]) - min(sata[j], sata[j + 1]) == 1:
			t = True
		elif max(sata[j], sata[j + 1]) == max(sata) and min(sata[j], sata[j + 1]) == min(sata):
			t = True
		else:
			t = False
			break
	if t:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split(' ')))
	if n == 1:
		print('YES')
	elif (p[1] - p[0]) % n != 1 and (p[0] - p[1]) % n != 1:
		print('NO')
	else:
		d = (p[1] - p[0]) % n
		cond = True
		for k in range(n - 1):
			if (p[k + 1] - p[k]) % n != d:
				cond = False
				break
		print('YES' if cond else 'NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = [int(item) for item in input().split(' ')]
	ans = True
	for i in range(n):
		if (arr[i - 1] + 1) % n == arr[i] % n:
			continue
		ans = False
		break
	ans2 = True
	for i in range(n):
		if (arr[i] + 1) % n == arr[i - 1] % n:
			continue
		ans2 = False
		break
	print('YES' if ans or ans2 else 'NO')


for _ in range(int(input())):
	n = int(input())
	perm = list(map(int, input().split())) * 2
	if n == 1:
		print('YES')
		continue

	def find():
		i = 0
		while perm[i] != 1:
			i += 1
		return i

	def check(i):
		cnt = 1
		while perm[i] + 1 == perm[i + 1] and i + 1 < 2 * n:
			cnt += 1
			i += 1
		if cnt == n:
			return True
		else:
			return False
	ans: str
	if check(find()):
		ans = 'YES'
	else:
		perm.reverse()
		if check(find()):
			ans = 'YES'
		else:
			ans = 'NO'
	print(ans)


import sys
import math

def process_test():
	num_students = int(inp.readline().rstrip())
	students = list(map(int, inp.readline().split()))
	solve(students, num_students)

def solve(students, N):
	if N == 1:
		print('YES')
		return
	seen_end = False
	for i in range(N):
		if i == N - 1:
			diff = math.fabs(students[i] - students[0])
		else:
			diff = math.fabs(students[i] - students[i + 1])
		if seen_end:
			if diff != 1:
				print('NO')
				return
		else:
			seen_end = diff != 1
	print('YES')
inp = sys.stdin
num_tests = int(inp.readline().rstrip())
for _ in range(num_tests):
	process_test()


def reverse(a):
	reversed = [a[0]]
	for j in range(len(a) - 1, 0, -1):
		reversed.append(a[j])
	return reversed

def putin():
	n = int(input())
	a = [int(x) for x in input().split()]
	for s in range(len(a)):
		if a[s] == 1:
			one = s
	new_a = []
	for i in range(one, len(a)):
		new_a.append(a[i])
	for i in range(0, one):
		new_a.append(a[i])
	return new_a

def main(new_a):
	for t in range(len(new_a) - 1):
		if new_a[t] + 1 != new_a[t + 1]:
			return False
	return True
q = int(input())
for u in range(q):
	arr = putin()
	if main(arr) == False:
		if main(reverse(arr)) == False:
			print('NO')
		else:
			print('YES')
	else:
		print('YES')


q = int(input())
f = []
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		f += ['YES']
		continue
	k = 'YES'
	for i in range(n - 1):
		c = (a[i] + 1) % n
		if c == 0:
			c = n
		if a[i + 1] != c:
			k = 'NO'
			break
	if k == 'NO':
		k = 'YES'
		for i in range(n - 1):
			c = (a[i] - 1) % n
			if c == 0:
				c = n
			if c != a[i + 1]:
				k = 'NO'
				break
	f += [k]
for i in f:
	print(i)


q = int(input())
for i in range(q):
	n = int(input())
	r = list(map(int, input().split()))
	r += 2 * r
	for j in range(n, 2 * n):
		if r[j] == 1:
			p = j
			break
	x = 1
	c = True
	while x != n:
		if x - r[p + 1] == -1:
			x = r[p + 1]
			p += 1
		elif x - r[p - 1] == -1:
			x = r[p - 1]
			p -= 1
		else:
			c = False
			break
	if c:
		print('YES')
	else:
		print('NO')


q = int(input())
b = [-1] * q
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n > 3:
		f1 = 1
		for j in range(1, n):
			if a[j - 1] + 1 != a[j] and a[j] != 1:
				f1 = 0
		f2 = 1
		for j in range(1, n):
			if a[j - 1] - 1 != a[j] and a[j - 1] != 1:
				f2 = 0
		if f1 == 1 or f2 == 1:
			b[i] = 1
		else:
			b[i] = 0
	else:
		b[i] = 1
for i in range(q):
	if b[i] == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	no = int(input())
	l = list(map(int, input().split()))
	check = 1
	if l[len(l) - 1] - l[0] == 1 or l[len(l) - 1] - l[0] == -1 or l[len(l) - 1] - l[0] == no - 1 or (l[len(l) - 1] - l[0] == 1 - no):
		check = 0
		for x in range(len(l) - 1):
			if l[x + 1] - l[x] == 1 or l[x + 1] - l[x] == -1:
				check = 0
			elif l[x + 1] - l[x] == no - 1 or l[x + 1] - l[x] == 1 - no:
				check = 0
			else:
				check = 1
				break
	else:
		check = 1
	if check == 0:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		d = set()
		for j in range(n):
			if j < n - 1:
				d.add(a[j] - a[j + 1])
			else:
				d.add(a[j] - a[0])
		if len(d) <= 2 and (d == {-1, n - 1} or d == {1, 1 - n}):
			print('YES')
		else:
			print('NO')


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	s = get_list()
	i = s.index(1)
	start = (i + 1) % n
	nextNum = 2
	clock = True
	while start != i:
		if nextNum != s[start]:
			clock = False
			break
		else:
			start = (start + 1) % n
			nextNum += 1
	start = (i - 1) % n
	nextNum = 2
	anticlock = True
	while start != i:
		if nextNum != s[start]:
			anticlock = False
			break
		else:
			nextNum += 1
			start = (start - 1) % n
	if clock or anticlock:
		print('YES')
	else:
		print('NO')


from collections import defaultdict
t = int(input())
for _ in range(t):
	k = int(input())
	arr = list(map(int, input().split()))
	count = 0
	w = 1 - k
	q = k - 1
	d = defaultdict(int)
	for i in range(k):
		d[arr[i] - arr[(i + 1) % k]] += 1
	if d[-1] + d[q] == k or d[1] + d[w] == k:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	students = [int(x) for x in input().split()]
	if n < 4:
		print('YES')
		continue
	i = students.index(1)
	students += students
	success = 1
	direction = students[i + 2] - students[i + 1]
	if abs(direction) != 1:
		success = 0
	for j in range(i + 2, i + n):
		if students[j] - students[j - 1] != direction:
			success = 0
			break
	print('YES' if success else 'NO')


import math
import sys
from collections import defaultdict

def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		p = list(map(int, input().split()))
		if n < 3:
			print('YES')
		else:
			ok = True
			pos = 0
			while p[pos] != 1:
				pos += 1
			c = p[pos:] + p[0:pos]
			for i in range(n - 1):
				if c[i] + 1 != c[i + 1]:
					ok = False
					break
			if not ok:
				ok = True
				pos = 0
				while p[pos] != n:
					pos += 1
				cc = p[pos:] + p[0:pos]
				for i in range(n - 1):
					if cc[i] - 1 != cc[i + 1]:
						ok = False
						break
			print('YES' if ok else 'NO')
main()


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	i = a.index(1)
	k = True
	if n == 1:
		print('YES')
		continue
	elif a[i - 1] == 2:
		for _ in range(n - 1):
			if a[i % n] - a[(i - 1) % n] != -1:
				print('NO')
				k = False
				break
			i = i - 1
	else:
		for _ in range(n - 1):
			if a[(i + 1) % n] - a[i % n] != 1:
				print('NO')
				k = False
				break
			i = i + 1
	if k:
		print('YES')


from math import ceil, sqrt, floor
import sys
n = int(input())
for i in range(n):
	a = int(input())
	stud = list(map(int, input().split()))
	stud2 = stud[::-1]
	index = stud.index(1)
	rep = True
	rep2 = True
	for j in range(index, index + a - 1):
		if stud[j % a] + 1 != stud[(j + 1) % a]:
			rep = False
	for j in range(stud2.index(1), stud2.index(1) + a - 1):
		if stud2[j % a] + 1 != stud2[(j + 1) % a]:
			rep2 = False
	if rep or rep2:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	flag = 0
	a = list(map(int, input().split()))
	b = sorted(a)
	b1 = b[-1::-1]
	a += a
	for i in range(n):
		if a[i:i + n] == b or a[i:i + n] == b1:
			flag = 1
			break
	if flag == 0:
		print('NO')
	else:
		print('YES')


import sys
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
T = int(input())
while T > 0:
	n = int(input())
	Arr = get_array()
	flag = 0
	for i in range(n - 1):
		if abs(Arr[i] - Arr[i + 1]) != 1:
			flag += 1
	if abs(Arr[n - 1] - Arr[0]) != 1:
		flag += 1
	if flag <= 1:
		print('YES')
	else:
		print('NO')
	T -= 1


import sys
import math
import bisect

def solve(A):
	n = len(A)
	for i in range(n):
		B = A[i:] + A[:i]
		if B == sorted(A):
			return True
		if B == sorted(A, reverse=True):
			return True
	return False

def main():
	for _ in range(int(input())):
		n = int(input())
		A = list(map(int, input().split()))
		if solve(A):
			print('YES')
		else:
			print('NO')
main()


q = int(input())
for testcases in range(q):
	n = int(input())
	P = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	for i in range(1, n):
		if P[i] - P[i - 1] % n == 1:
			continue
		else:
			break
	else:
		print('YES')
		continue
	for i in range(n - 1):
		if P[i] - P[i + 1] % n == 1:
			continue
		else:
			break
	else:
		print('YES')
		continue
	print('NO')


def f(arr):
	count = 0
	length = len(arr)
	for i in range(length):
		if arr[i] > arr[(i + 1) % len(arr)]:
			count += 1
	return count <= 1

def g(lst):
	h = lst[:]
	h.sort()
	return h == lst
a = int(input())
for tc in range(a):
	n = int(input())
	c = list(map(int, input().split()))
	d = c[:]
	d.reverse()
	if g(c) or g(d):
		print('YES')
	elif f(c) or f(d):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	flag = True
	if arr[0] > arr[1]:
		diff = 1
	else:
		diff = -1
	for i in range(n - 1):
		if diff != arr[i] - arr[i + 1]:
			if abs(arr[i] - arr[i + 1]) != n - 1:
				flag = False
				break
			elif arr[i] == n:
				diff = -1
			else:
				diff = 1
	if flag:
		print('YES')
	else:
		print('NO')


z = input
from math import ceil
n = int(z())
for i in range(n):
	x = int(z())
	l = list(map(int, z().split()))
	c = 0
	d = 0
	for i in range(x):
		if l[i] > l[i - 1]:
			c += 1
		else:
			d += 1
	if c > d and d < 2 or (d > c and c < 2) or x == 2:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	i = a.index(1)
	count = 1
	while count < n and a[(i + 1) % n] == a[i] + 1:
		count += 1
		i = (i + 1) % n
	if count >= n:
		print('YES')
		continue
	i = a.index(1)
	count = 1
	while count < n and a[(i - 1) % n] == a[i] + 1:
		count += 1
		i = (i - 1) % n
	if count >= n:
		print('YES')
		continue
	print('NO')


q = int(input())
for query in range(q):
	n = int(input())
	A = list(map(int, input().split()))
	num = 0
	while A[num] != 1:
		num += 1
	if A[num:] + A[:num] == [i for i in range(1, n + 1)] or A[num:] + A[:num] == [1] + [i for i in range(n, 1, -1)]:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	result = None
	p2 = 2 * p
	for pivot in [1, n]:
		ipivot = p2.index(pivot)
		segment = p2[ipivot:ipivot + n]
		if pivot == 1 and segment == list(range(1, n + 1)):
			result = 'YES'
			break
		if pivot == n and segment == list(reversed(range(1, n + 1))):
			result = 'YES'
			break
		else:
			result = 'NO'
	print(result)


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	anomaly = 0
	if n == 1:
		print('YES')
		continue
	if n == 2:
		arr.sort()
		if arr == [1, 2]:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(n - 1):
		if abs(arr[i + 1] - arr[i]) != 1:
			anomaly += 1
	if abs(arr[-1] - arr[0]) != 1:
		anomaly += 1
	if anomaly == 1:
		print('YES')
	else:
		print('NO')


def f(a):
	b = a[:]
	b.sort()
	return a == b or a == b[::-1]
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = a[0]
	j = n - a[0] - 1
	c = [(x - i) % n + 1 for x in a]
	d = [(x + j) % n + 1 for x in a]
	print('YES' if f(c) or f(d) else 'NO')


for _ in range(int(input())):
	n = int(input())
	p = list((int(a) for a in input().split()))
	c = 0
	if p[n - 1] < p[0]:
		c += 1
	for i in range(1, n):
		if p[i - 1] < p[i]:
			c += 1
	if c == n - 1 or c == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for Q in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	best1 = [i for i in range(1, n + 1)]
	best2 = [i for i in range(n, 0, -1)]
	arr = arr + arr
	flag = False
	for i in range(len(arr) - n):
		if arr[i:i + n] == best1 or arr[i:i + n] == best2:
			print('YES')
			flag = True
			break
	if not flag:
		print('NO')


from collections import Counter
import math

def getlist():
	return list(map(int, input().split()))

def getvalue():
	return int(input())

def getstring():
	return input()

def clock(l):
	for i in range(1, len(l)):
		if l[i] - 1 != l[i - 1] and l[i] != 1:
			return False
	return True

def anticlock(l):
	for i in range(1, len(l)):
		if l[i] + 1 != l[i - 1] and l[i] != len(l):
			return False
	return True
for _ in range(int(input())):
	n = getvalue()
	l = getlist()
	if clock(l) or anticlock(l):
		print('YES')
	else:
		print('NO')


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(A):
	for i in range(N):
		if (A[i] + 1) % N != A[(i + 1) % N]:
			return False
	return True
for _ in range(INT()):
	N = INT()
	A = [a - 1 for a in LIST()]
	if check(A) or check(A[::-1]):
		YES()
	else:
		NO()


import sys, math, bisect

def pow(a, b):
	res = 1
	while b > 0:
		if b & 1:
			res *= a
		a *= a
		b >>= 1
	return res

def powmod(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a *= a
		b >>= 1
	return res

def inv(a, m):
	return powmod(a, m - 2, m)

def check(p):
	n = len(p)
	res = 0
	for i in range(1, n):
		if abs(p[i] - p[i - 1]) == 1:
			res += 1
	if abs(p[n - 1] - p[0]) == 1:
		res += 1
	if res == n - 1:
		return True
	if n == 2 and res == 2:
		return True
	return False
q = int(input())
for i in range(q):
	n = int(input())
	p = [int(s) for s in input().split(' ')]
	if check(p):
		print('YES')
	else:
		print('NO')


queryTot = int(input())
out = ''
for queryIndex in range(queryTot):
	n = int(input())
	queryInp = input()
	query = [int(elem) for elem in queryInp.split(' ')]
	nIndex = query.index(n)
	if n in (1, 2, 3):
		out = out + 'YES\n'
	elif query[1] == query[0] + 1 or (nIndex == 0 and query[1] == 1):
		replace = [elem + n for elem in query[nIndex + 1:]]
		query[nIndex + 1:] = replace
		queryDiff = [fin - init for (init, fin) in zip(query[:-1], query[1:])]
		if all([elem == 1 for elem in queryDiff]):
			out = out + 'YES\n'
		else:
			out = out + 'NO\n'
	elif query[1] == query[0] - 1 or (nIndex == 1 and query[0] == 1):
		replace = [elem + n for elem in query[:nIndex]]
		query[:nIndex] = replace
		queryDiff = [fin - init for (init, fin) in zip(query[:-1], query[1:])]
		if all([elem == -1 for elem in queryDiff]):
			out = out + 'YES\n'
		else:
			out = out + 'NO\n'
	else:
		out = out + 'NO\n'
cleaned = out.rstrip()
print(cleaned)


q = int(input())
for i in range(q):
	n = int(input())
	P = list(map(int, input().split()))
	a = 0
	for i in range(n - 1):
		if abs(P[i + 1] - P[i]) == 1:
			a += 1
	if abs(P[-1] - P[0]) == 1:
		a += 1
	if n > 2 or n == 1:
		if a == n - 1:
			print('YES')
		else:
			print('NO')
	elif n == 2:
		if a == n:
			print('YES')
		else:
			print('NO')


q = int(input())
for j in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	ok = 0
	m = n - 1
	for i in range(n - 1):
		if abs(a[i + 1] - a[i]) != 1 and abs(a[i + 1] - a[i]) != m:
			print('NO')
			ok = 1
			break
	if ok == 0:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = [*map(int, input().split())]
	res = False
	for i in range(n):
		x = l[i:] + l[:i]
		if all((x[i] < x[i + 1] for i in range(n - 1))) or all((x[i] > x[i + 1] for i in range(n - 1))):
			res = True
	print('YES' if res else 'NO')


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	P = list(map(int, input().split()))
	ans = True
	minimum = P[0]
	maximum = P[0]
	for i in range(n):
		minimum = min(P[i], minimum)
		maximum = max(P[i], maximum)
	for i in range(1, n):
		diff = abs(P[i] - P[i - 1])
		if diff > 1 and diff != maximum - minimum:
			ans = False
			print('NO')
			break
	if ans:
		print('YES')


q = int(input())
for i in range(q):
	c = 0
	k = 0
	n = int(input())
	a = list(map(int, input().split()))
	x = a.index(1)
	y = a[x:] + a[:x]
	z = max(a) + 1
	for j in y:
		k += 1
		if j != k:
			c += 1
			break
	f = a.index(max(a))
	e = a[f:] + a[:f]
	for m in e:
		z -= 1
		if m != z:
			c += 1
			break
	if c < 2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list((int(c) for c in input().split()))
	p = i = 0
	while i < n:
		if abs(l[i] - l[i - 1]) > 1:
			p += 1
		i += 1
	print('YES' if p < 2 else 'NO')


q = int(input())
for i in range(q):
	w = int(input())
	e = list(map(int, input().split()))
	r = sorted(e)
	k = 0
	for i in range(w):
		if e == r or e == r[::-1]:
			print('YES')
			k = 1
			break
		l = e.pop(0)
		e.append(l)
	if k == 0:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	A = [int(x) for x in input().split()]
	i = 0
	Ind = A.index(min(A))
	while i < n and A[Ind] == i + 1:
		Ind = (Ind + 1) % n
		i = i + 1
	if i == n:
		print('YES')
	else:
		Ind = A.index(min(A))
		i = 0
		while i < n and A[Ind] == i + 1:
			Ind = (Ind - 1) % n
			i = i + 1
		if i == n:
			print('YES')
		else:
			print('NO')


q = int(input())
st = [int(i) for i in range(1, 201)]
for i in range(q):
	n = int(input())
	c = [int(s) for s in input().split()]
	st1 = st[:n]
	st2 = st1[::-1]
	per = ''
	for j in range(0, n):
		r = c[j:] + c[:j]
		if r == st1 or r == st2:
			print('YES')
			per = '1'
			break
	if per == '':
		print('NO')


Q = int(input())
for _ in range(Q):
	N = int(input())
	A = [int(a) for a in input().split()]
	B = [(A[i] - A[i - 1]) % N for i in range(N - 1)]
	if N <= 3 or B.count(1) == N - 1 or B.count(N - 1) == N - 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	inp = list(map(int, input().split()))
	j = inp.index(min(inp))
	l = inp
	if j != 0 or j != len(inp) - 1:
		if inp[j - 1] == 2:
			x = [i for i in range(1, j + 2)]
			x.reverse()
			y = [i for i in range(j + 2, n + 1)]
			y.reverse()
			if l[:j + 1] == x and l[j + 1:] == y:
				print('YES')
			else:
				print('NO')
		else:
			x = [i for i in range(1, n - j + 1)]
			y = [i for i in range(n - j + 1, n + 1)]
			if x == l[j:] and y == l[:j]:
				print('YES')
			else:
				print('NO')
	elif j == 0:
		x = l[1:]
		a = sorted(x)
		b = list(reversed(x))
		if x == a or x == b:
			print('YES')
		else:
			print('NO')
	elif j == len(inp) - 1:
		x = l[:n - 1]
		a = sorted(x)
		b = list(reversed(x))
		if x == a or x == b:
			print('YES')
		else:
			print('NO')


stored_values = []
number_of_queries = int(input())
for i in range(number_of_queries):
	number_of_students = int(input())
	permutations_of_students = input().split()
	for i in range(number_of_students):
		permutations_of_students[i] = int(permutations_of_students[i])
	one_starts_at = permutations_of_students.index(1)
	forward_works = 'YES'
	backward_works = 'YES'
	for i in range(number_of_students - 1):
		forward_index = (one_starts_at + i + 1) % number_of_students
		backward_index = (one_starts_at - i - 1) % number_of_students
		if not permutations_of_students[forward_index] == i + 2:
			forward_works = 'NO'
		if not permutations_of_students[backward_index] == i + 2:
			backward_works = 'NO'
	if forward_works == 'YES' or backward_works == 'YES':
		print('YES')
	else:
		print('NO')


for j in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	p = 0
	if n == 1:
		if l[0] == 1:
			print('YES')
		else:
			print('NO')
		p = 1
	for i in range(n - 1):
		if abs(l[i] - l[i - 1]) == 1 or abs(l[i] - l[i - 1]) == n - 1:
			pass
		else:
			print('NO')
			p = 1
			break
	if p == 0:
		if abs(l[0] - l[n - 1]) == 1 or abs(l[0] - l[n - 1]) == n - 1:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	while i < n:
		if a[i] == 1:
			break
		i += 1
	b = a[i:] + a[:i]
	if i != n:
		c = a[i + 1:] + a[:i + 1]
	else:
		c = a
	a1 = sorted(a)
	a2 = a1[::-1]
	if a1 == b or a2 == c:
		print('YES')
	else:
		print('NO')


Q = int(input())
for q in range(Q):
	n = int(input())
	d = list(map(int, input().split()))
	f = True
	for j in range(n):
		p = d[j:] + d[:j]
		q = [i + 1 for i in range(n)]
		if q == p:
			print('YES')
			f = False
			break
		q = [n - i for i in range(n)]
		if q == p:
			print('YES')
			f = False
			break
	if f == True:
		print('NO')


def function2():
	queries = int(str(input()))
	for _ in range(queries):
		ignore = input()
		circle = [int(x) for x in str(input()).split()]
		start = circle.index(1)
		failed = False
		next_index = (start + 1) % len(circle)
		prev_index = (start - 1) % len(circle)
		if len(circle) == 1:
			print('YES')
			continue

		def loop(index, increment, start):
			while index != start:
				prev_index = (index - increment) % len(circle)
				if circle[index] < circle[prev_index]:
					return True
				index = (index + increment) % len(circle)
			return False
		if circle[next_index] == 2:
			failed = loop(next_index, 1, start)
		else:
			failed = loop(prev_index, -1, start)
		if failed:
			print('NO')
		else:
			print('YES')
function2()


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	one = a.index(1)
	idx = one
	cc = True
	for j in range(n - 1):
		if a[idx] + 1 != a[(idx + 1) % n]:
			cc = False
			break
		idx = (idx + 1) % n
	ccc = True
	idx = one
	for j in range(n - 1):
		if a[idx] + 1 != a[(idx - 1 + n) % n]:
			ccc = False
			break
		idx = (idx - 1 + n) % n
	if ccc or cc:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	m = int(input())
	(*a,) = map(int, input().split())
	b = list(range(a[0], m + 1)) + list(range(1, a[0]))
	c = list(range(a[0], 0, -1)) + list(range(m, a[0], -1))
	if a == b or a == c:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	for j in range(len(l) - 1):
		r = abs(l[j] - l[j + 1])
		if r == 1 or r == n - 1:
			continue
		else:
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')


from math import *
from itertools import *
from collections import *
from bisect import *
q = int(input())
for _ in range(q):
	n = int(input())
	lst = [int(x) for x in input().split()]
	idx = lst.index(1)
	srt = lst[idx:] + lst[:idx]
	srv = lst[idx::-1] + lst[:idx:-1]
	lst.sort()
	if srt == lst or srv == lst:
		print('YES')
	else:
		print('NO')


def func1(z, n):
	l = []
	for i in range(1, n + 1):
		l.append(i)
	if z == l or z == l[::-1]:
		return True
q = int(input())
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	if func1(l, n):
		print('YES')
	else:
		for i in range(n):
			l = [l[-1]] + l[0:-1]
			if func1(l, n):
				print('YES')
				break
		else:
			print('NO')


import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
	n = int(input())
	A = [int(i) for i in input().split()]
	a = A[0]
	(A1, A2) = ([0] * n, [0] * n)
	(A1[0], A2[0]) = (a, a)
	for i in range(1, n):
		if a + i > n:
			A1[i] = a + i - n
		else:
			A1[i] = a + i
		if a - i <= 0:
			A2[i] = a - i + n
		else:
			A2[i] = a - i
	if A == A1 or A == A2:
		print('YES')
	else:
		print('NO')


q = int(input())
m = []
for i in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	prev = 0
	for i in range(n):
		if a[i] == 1:
			prev = i
			break
	cnt = 0
	nextt = prev
	set_dir = False
	dr = 0
	for j in range(n):
		nextt = (prev + 1) % n
		if set_dir == False:
			if a[nextt] == 2:
				dr = 1
			elif a[nextt] == n:
				dr = -1
			else:
				break
			set_dir = True
			cnt += 1
		elif a[prev] == n and a[nextt] == 1 and (dr == 1) or (a[prev] == 1 and a[nextt] == n and (dr == -1)) or a[nextt] - a[prev] == dr:
			cnt += 1
		prev = nextt
	if cnt == n:
		m.append('YES')
	else:
		m.append('NO')
for i in m:
	print(i)


for i in range(int(input())):
	sum1 = 0
	sum2 = 0
	b = 0
	n = int(input())
	a = [int(i) for i in input().split()]
	for j in range(n):
		if not a[j - 1] < a[j] < a[(j + 1) % n]:
			sum1 += 1
		if a[j] == 1:
			if not (a[j - 1] == n or a[(j + 1) % n] == n):
				b = 1
	for j in range(n):
		if not a[j - 1] > a[j] > a[(j + 1) % n]:
			sum2 += 1
	if (sum1 <= 2 or sum2 <= 2) and b == 0:
		print('YES')
	else:
		print('NO')


def check(rl):
	k = 0
	r = len(rl)
	i = rl.index(min(rl))
	while k < len(rl) - 1:
		if rl[(i + 1) % r] - rl[i % r] != 1:
			return 'NO'
		i += 1
		k += 1
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	a = input().split()
	l = []
	k = []
	for i in a:
		l.append(int(i))
	r = len(l)
	rl = l
	i1 = l.index(min(l))
	res = 'NO'
	if l[(i1 + 1) % r] == max(l):
		rl = l[::-1]
		i1 = rl.index(min(rl))
		res = check(rl)
	elif l[(i1 - 1) % r] == max(l):
		res = check(rl)
	print(res)
	l.clear()
	a.clear()


import copy
n = int(input())
g = []
for i in range(n):
	res = 'No'
	s = []
	c = int(input())
	g = list(map(int, input().split()))
	s = copy.deepcopy(g)
	s.sort()
	for j in range(c):
		l = g[0]
		for v in range(c - 1):
			g[v] = g[v + 1]
		g[c - 1] = l
		if s == g or g == s[::-1]:
			res = 'Yes'
	print(res)


tc = int(input())
for _ in range(tc):
	n = int(input())
	l = list(map(int, input().split()))
	ans = ['YES', 'YES']
	one = l.index(1)
	i = one
	cnt = 0
	while cnt < n - 1:
		tmp = (i + 1) % n
		if l[tmp] - l[i] == 1:
			i = tmp
		else:
			ans[0] = 'NO'
			break
		cnt += 1
	i = one
	cnt = 0
	while cnt < n - 1:
		tmp = i - 1
		if l[tmp] - l[i] == 1:
			i = tmp
		else:
			ans[1] = 'NO'
			break
		cnt += 1
	if 'YES' in ans:
		print('YES')
	else:
		print('NO')


q = int(input())
for query in range(q):
	n = int(input())
	s = list(map(int, input().split()))
	(flagC, flagCC) = (True, True)
	for i in range(n - 1):
		if s[i + 1] != 1 and s[i + 1] - s[i] != 1:
			flagC = False
			break
	for i in range(1, n):
		if s[i - 1] != 1 and s[i - 1] - s[i] != 1:
			flagCC = False
			break
	if flagC or flagCC:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	x = int(input())
	y = list(map(int, input().split()))
	a = list(y)
	a.sort()
	min = a[0]
	yu = 1
	y.extend(y)
	for i in range(x):
		if y[i:i + x] == a or y[i:i + x] == a[::-1]:
			yu = 0
			print('YES')
			break
		if y[i - x:2 * x - i] == a[::-1] or y[i - x:2 * x - i] == a:
			yu = 0
			print('YES')
			break
	if yu == 1:
		print('NO')


q = int(input())
n = []
table_pos = []
for i in range(q):
	n.append(int(input()))
	table_pos.append(list(map(int, input().split())))
for j in range(q):
	Check_clock = 1
	Check_anticlock = 1
	temp = 1
	count = n[j]
	table = table_pos[j]
	x = table.index(1)
	if x == count - 1:
		for v in range(x):
			if table[v] - table[v + 1] != 1:
				Check_anticlock = 0
				break
	else:
		for k in range(x, count - 1):
			if table[k + 1] - table[k] != 1:
				Check_clock = 0
			if k != x and table[k] - table[k + 1] != 1:
				Check_anticlock = 0
	if Check_clock != 0 and x != 0:
		if table[0] - table[count - 1] != 1:
			Check_clock = 0
		else:
			for g in range(x - 1):
				if table[g + 1] - table[g] != 1:
					Check_clock = 0
					break
	if Check_anticlock != 0 and x != 0:
		if x != count - 1 and table[count - 1] - table[0] != 1:
			Check_anticlock = 0
		else:
			for h in range(x - 1):
				if table[h] - table[h + 1] != 1:
					Check_anticlock = 0
					break
	if Check_clock == 1 or Check_anticlock == 1:
		print('YES')
		temp = 0
	if temp == 1:
		print('NO')


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	a = [int(x) for x in stdin.readline().split()]
	if all([abs(a[i] - a[i - 1]) in [1, n - 1] for i in range(n - 1, -1, -1)]):
		stdout.write('YES\n')
	else:
		stdout.write('NO\n')


tc = int(input())
while tc != 0:
	n = int(input())
	lst = list(map(int, input().split()))
	i = lst.index(1)
	ic = i
	a = [0] * n
	j = 0
	while j < n:
		a[i] = j + 1
		i += 1
		i %= n
		j += 1
	b = [0] * n
	i = ic
	j = 0
	while j < n:
		b[i] = j + 1
		i -= 1
		if i == -1:
			i = n - 1
		j += 1
	ktr = [0] * 1000
	c = a + b
	if lst == a or lst == b:
		print('YES')
	else:
		print('NO')
	tc -= 1


t = int(input())
res = []
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	s = a.index(1)
	f = 1
	nxt = (s + 1) % n
	if a[nxt] - a[s] == 1:
		nxt = (s + 1) % n
		while a[nxt] != 1:
			if a[nxt] - a[s] == 1:
				s = (s + 1) % n
			else:
				res.append('NO')
				f = 0
				break
			nxt = (s + 1) % n
	else:
		nxt = s - 1
		if nxt < 0:
			nxt = n - 1
		while a[nxt] != 1:
			if a[nxt] - a[s] == 1:
				s -= 1
				if s < 0:
					s = n - 1
			else:
				res.append('NO')
				f = 0
				break
			nxt = s - 1
			if nxt < 0:
				nxt = n - 1
	if f == 1:
		res.append('YES')
for i in res:
	print(i)


import math
for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		c = 0
		f = 0
		for i in range(1, n):
			if 1 < abs(l[i] - l[i - 1]) < n - 1:
				f = 1
				break
		if f:
			print('NO')
		else:
			print('YES')


q = int(input())
for qq in range(q):
	n = int(input())
	(*p,) = map(int, input().split())
	for i in range(n - 1):
		if p[i + 1] - p[i] != 1 and (not (p[i + 1] == 1 and p[i] == n)):
			break
	else:
		print('YES')
		continue
	for i in range(n - 1):
		if p[i + 1] - p[i] != -1 and (not (p[i + 1] == n and p[i] == 1)):
			break
	else:
		print('YES')
		continue
	print('NO')


q = int(input())
for i in range(q):
	T = True
	t = True
	n = int(input())
	p = list(map(int, input().split()))
	if n == 1:
		t = True
		T = False
	if T:
		for i in range(n - 1):
			if p[i + 1] - p[i] != 1 and (p[i + 1] != 1 or p[i] != n):
				t = False
				T = True
				break
	if t:
		T = False
	if T:
		t = True
		for i in range(n - 1):
			if p[i] - p[i + 1] != 1 and (p[i] != 1 or p[i + 1] != n):
				t = False
				break
	if not t:
		print('NO')
	else:
		print('YES')


import math

class Read:

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	n = Read.int()
	a = Read.list_int()
	first_index = a.index(1)
	if a[first_index - 1] == 2:
		f = 1
		for i in range(first_index, -1 * (n - first_index), -1):
			if a[i] != f:
				print('NO')
				return
			f += 1
	else:
		f = 1
		for i in range(first_index, n + first_index):
			if a[i % n] != f:
				print('NO')
				return
			f += 1
	print('YES')
query_count = Read.int()
for j in range(query_count):
	solve()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	idx = l.index(1)
	f = 0
	if n == 1:
		if l[0] == 1:
			print('YES')
		else:
			print('NO')
		continue
	elif idx == n - 1:
		if l[idx - 1] == 2:
			l = l[::-1]
		elif l[idx - 1] == n:
			l = [1] + l[:n - 1]
		else:
			print('NO')
			continue
	elif l[idx + 1] == n:
		l = l[:idx + 1][::-1] + l[idx + 1:][::-1]
	elif l[idx + 1 == 2]:
		l = l[idx:] + l[:idx]
	else:
		f = 1
	for i in range(1, n):
		if l[i] != l[i - 1] + 1:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


num = int(input())
first = []
i = 0
arr = []
flag = 0
size = 0
while i < num:
	size = int(input())
	arr = list(map(int, input().split()))
	if size == 1:
		print('YES')
	else:
		b = 1
		anti = 0
		clock = 0
		n = len(arr)
		k = arr.index(b)
		j = arr.index(n)
		if arr[j - 1] == 1:
			anti = 1
		elif arr[k - 1] == n:
			clock = 1
		if anti == 1:
			p = 1
			q = len(arr)
			low = arr.index(p)
			high = arr.index(q)
			if arr[high - 1] == 1:
				if low == 0:
					anti = 2
				while low > 0:
					if arr[low] + 1 == arr[low - 1]:
						anti = 2
						low = low - 1
					else:
						anti = 0
						break
				if anti == 2:
					while high <= size - 2:
						if arr[high] - 1 == arr[high + 1]:
							anti = 2
							high = high + 1
						else:
							anti = 0
							break
			else:
				anti = 0
				break
		elif clock == 1:
			p = 1
			q = len(arr)
			low = arr.index(p)
			high = arr.index(q)
			if arr[low - 1] == q:
				if low == size - 1:
					clock = 2
				else:
					clock = 0
				while low <= size - 2:
					if arr[low] + 1 == arr[low + 1]:
						clock = 2
						low = low + 1
					else:
						clock = 0
						break
				if clock == 2:
					while high > 0:
						if arr[high] - 1 == arr[high - 1]:
							clock = 2
							high = high - 1
						else:
							clock = 0
							break
			else:
				clock = 0
				break
		if clock == 2 or anti == 2:
			print('YES')
		else:
			print('NO')
	i = i + 1


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	b = 0
	c = 0
	for i in range(n - 1):
		if abs(a[i + 1] - a[i]) == 1:
			b += 1
		elif abs(a[i + 1] - a[i]) == n - 1:
			c += 1
	if b == n - 1 and c == 0 or (b == n - 2 and c == 1):
		print('YES')
	else:
		print('NO')


def rotate(a, n):
	return a[n:] + a[:n]
from collections import deque
q = int(input())
for w in range(q):
	n = int(input())
	a = [int(i) for i in input().split()]
	a = rotate(a, a.index(1))
	a1 = iter(a)
	a2 = iter(a)
	next(a2)
	c = 1
	for (i, j) in zip(a1, a2):
		if j - i != 1:
			c = -1
			break
	if c == -1:
		a = rotate(a, a.index(n))
		a1 = iter(a)
		a2 = iter(a)
		next(a2)
		for (i, j) in zip(a1, a2):
			if j - i != -1:
				c = 0
				break
	if c == 1 or c == -1:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
from collections import deque
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	arr = deque(map(int, input().split()))
	flag = True
	ind = arr.index(1)
	if arr[(ind + 1) % n] == 2:
		for i in range(n):
			if arr[(ind + i) % n] != i + 1:
				flag = False
	elif arr[ind - 1] == 2:
		for i in range(n):
			if arr[ind - i] != i + 1:
				flag = False
	elif n != 1:
		flag = False
	print('YES' if flag else 'NO')


t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * n
	for i in range(n):
		b[a[i] - 1] = i + 1
	k = 0
	flag = 0
	if n == 1:
		print('YES')
	elif abs(b[0] - b[1]) == 1 or abs(b[0] - b[1]) == n - 1:
		if abs(b[0] - b[1]) == 1:
			k = b[0] - b[1]
		else:
			k = b[1] - b[2]
		for j in range(1, n):
			if j == n - 1:
				if b[j] - b[0] == k or abs(b[j] - b[0]) == n - 1:
					print('YES')
			elif b[j] - b[j + 1] != k and abs(b[j] - b[j + 1]) != n - 1:
				flag = 1
				break
		if flag:
			print('NO')
	else:
		print('NO')
	t -= 1


q = int(input())
while q:
	n = int(input())
	a = list(map(int, input().split()))
	ok = True
	if n == 1:
		print('YES')
		ok = False
	a = a + a
	if ok:
		for (i, v) in enumerate(a):
			if v == n:
				diff = a[i + 2] - a[i + 1]
				if abs(diff) != 1:
					print('NO')
					ok = False
					break
				for j in range(i + 2, i + n):
					if a[j] - a[j - 1] != diff:
						print('NO')
						ok = False
						break
				break
	if ok:
		print('YES')
	q -= 1


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = a.copy()
	ind = a.index(1)
	c1 = c1[ind:] + c1[:ind]
	t1 = True
	for i in range(1, len(c1)):
		if c1[i] == c1[i - 1] + 1:
			pass
		else:
			t1 = False
			break
	t2 = True
	c2 = a.copy()
	c2.reverse()
	ind = c2.index(1)
	c2 = c2[ind:] + c2[:ind]
	for i in range(1, len(c2)):
		if c2[i] == c2[i - 1] + 1:
			pass
		else:
			t2 = False
			break
	if t1 or t2:
		print('YES')
	else:
		print('NO')


q = int(input())
for j in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	x = a.index(min(a))
	flag = 1
	for i in range(n - 1):
		if a[(x + i) % n] + 1 == a[(x + i + 1) % n]:
			flag = 1
		else:
			flag = 0
			break
	if flag == 0:
		for i in range(n - 1):
			if a[x - i] + 1 == a[x - i - 1]:
				flag = 1
			else:
				flag = 0
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


def game(arr, n):
	if n == 1 or n == 2:
		return 'YES'
	else:
		for i in range(n - 1):
			if abs(arr[i] - arr[i + 1]) == 1:
				continue
			else:
				arr = arr[i + 1:] + arr[:i + 1]
		if arr == list(range(1, n + 1)) or arr == list(range(n, 0, -1)):
			return 'YES'
		else:
			return 'NO'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	result = game(arr, n)
	print(result)


q = int(input())
for i in range(q):
	n = int(input())
	positions = list(map(int, input().split()))
	one_pos = positions.index(1)
	(left_s, left_s_r) = (positions[:one_pos + 1], positions[:one_pos + 1])
	(right_s, right_s_r) = (positions[one_pos + 1:], positions[one_pos + 1:])
	(left_p, left_p_r) = (positions[:one_pos], positions[:one_pos])
	(right_p, right_p_r) = (positions[one_pos:], positions[one_pos:])
	left_s_r.reverse()
	right_s_r.reverse()
	left_p_r.reverse()
	right_p_r.reverse()
	positions.sort()
	if positions == left_s + right_s or positions == left_s_r + right_s_r or positions == right_s + left_s or (positions == right_s_r + left_s_r) or (positions == left_p + right_p) or (positions == left_p_r + right_p_r) or (positions == right_p + left_p) or (positions == right_p_r + left_p_r):
		print('YES')
	else:
		print('NO')


def check_interval_c(list, first, last):
	for i in range(first, last - 1):
		if list[i] > list[i + 1]:
			return False
	return True
casos = int(input())
for i in range(casos):
	students = int(input())
	nums = [int(j) for j in input().split()]
	maior = 0
	menor = 0
	if students < 4:
		print('YES')
		continue
	for j in range(1, students):
		if nums[j] > nums[maior]:
			maior = j
		if nums[j] < nums[menor]:
			menor = j
	if maior == 0 and menor == students - 1:
		nums.reverse()
		maior = students - 1 - maior
		menor = students - 1 - menor
	if menor == 0 and maior == students - 1:
		if check_interval_c(nums, 0, students):
			print('YES')
		else:
			print('NO')
	elif abs(maior - menor) == 1:
		if maior > menor:
			nums.reverse()
			maior = students - 1 - maior
			menor = students - 1 - menor
		for k in range(maior + 1):
			a = nums.pop(0)
			nums.append(a)
		if check_interval_c(nums, 0, students):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(t,) = I()
while t:
	t -= 1
	(n,) = I()
	l = I()
	d = {}
	for i in range(n):
		k = l[i] - l[i - 1]
		d[k] = d.get(k, 0) + 1
	if n == 1 or n == 2 or (len(d) == 2 and (-1 in d and n - 1 in d or (1 in d and -n + 1 in d))):
		print('YES')
	else:
		print('NO')


for a in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	if len(p) < 4:
		print('YES')
		continue
	if p[p.index(1) - 1] == 2:
		p.reverse()
	p = p[p.index(1):] + p[:p.index(1)]
	if sorted(p) == p:
		print('YES')
	else:
		print('NO')


import itertools

def input_ints():
	return list(map(int, input().split()))

def output_list(v):
	print(' '.join((str(x) for x in v)))

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = input_ints()
		a += a
		found = False
		ok = (list(range(1, n + 1)), list(range(n, 0, -1)))
		for i in range(n):
			if a[i:i + n] in ok:
				found = True
				break
		print('YES' if found else 'NO')
main()


q = int(input())
l = []
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	r = 1
	for j in range(n - 1):
		if a[j] == n:
			if a[j + 1] != 1:
				r = 0
				break
		elif a[j] + 1 != a[j + 1]:
			r = 0
			break
	if r == 1:
		l.append('YES')
	else:
		r = 1
		for j in range(n - 1):
			if a[j] == 1:
				if a[j + 1] != n:
					r = 0
					break
			elif a[j] - 1 != a[j + 1]:
				r = 0
				break
		if r == 1:
			l.append('YES')
		else:
			l.append('NO')
for i in l:
	print(i)


def can_round_dance(n, a):
	if n == 1:
		return 'YES'
	if a[1] == a[0] % n + 1:
		if n == 2:
			return 'YES'
		for i in range(2, n):
			if a[i - 1] % n + 1 != a[i]:
				return 'NO'
		return 'YES'
	if a[1] % n + 1 == a[0]:
		if n == 2:
			return 'YES'
		for i in range(2, n):
			if a[i - 1] != a[i] % n + 1:
				return 'NO'
		return 'YES'
	return 'NO'
q = int(input())
for i in range(q):
	n = int(input())
	a = [int(e) for e in input().split()]
	print(can_round_dance(n, a))


def calc(n: int, arr: tuple):
	mn: int = min(arr)
	locMin: int = arr.index(mn)
	mx: int = max(arr)
	i: int = locMin
	prev: int = int()
	for _ in range(n):
		if i == n - 1:
			i = 0
		else:
			i += 1
		if _ == 0:
			prev = arr[i]
			continue
		else:
			if arr[i] == mn:
				break
			temp: int = abs(prev - arr[i])
			if temp != 1:
				return 'NO'
			else:
				prev = arr[i]
	return 'YES'

def main():
	for _ in range(int(input())):
		n: int = int(input())
		arr: tuple = tuple(map(int, input().split()))
		print(calc(n, arr))
main()


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	count = 0
	for x in range(n - 1):
		p = abs(l[x + 1] - l[x])
		if p != 1 and count == 0 and (p == n - 1):
			count += 1
		elif p != 1 and p != n - 1:
			print('NO')
			break
	else:
		print('YES')


for q in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	count = 0
	if n == 1 or n == 2 or n == 3:
		print('YES')
	else:
		diff = 0
		for i in range(n - 1):
			if p[i] - p[i + 1] == 1:
				diff = 1
				break
			elif p[i] - p[i + 1] == -1:
				diff = -1
				break
		for i in range(n - 1):
			if p[i] - p[i + 1] != diff:
				count += 1
		if count > 1:
			print('NO')
		else:
			print('YES')


from sys import stdin, stdout
INF = 100000000000.0

def get_int():
	return int(stdin.readline().strip())

def get_ints():
	return map(int, stdin.readline().strip().split())

def get_array():
	return list(map(int, stdin.readline().strip().split()))

def get_string():
	return stdin.readline().strip()
for _ in range(int(stdin.readline())):
	n = get_int()
	a = get_array()
	a1 = sorted(a)
	a2 = sorted(a, reverse=True)
	for i in range(n):
		a.append(a[i])
	for i in range(n):
		new = a[i:i + n]
		if new == a1 or new == a2:
			print('YES')
			break
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	li = [int(i) for i in input().split()]
	if n <= 2:
		print('YES')
	else:
		p = li.index(1)
		if li[p] + 1 == li[(p + 1) % n]:
			for i in range(1, n):
				if li[(p + i - 1) % n] + 1 != li[(p + i) % n]:
					print('NO')
					break
			else:
				print('YES')
		elif li[p] + 1 == li[(p - 1) % n]:
			for i in range(n, 1, -1):
				if li[(p + i - 1) % n] - 1 != li[(p + i) % n]:
					print('NO')
					break
			else:
				print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	(n, a) = (int(input()), list(map(int, input().split())))
	res = 'YES'
	if n > 3:
		if a[(a.index(n) - 1 + n) % n] == 1:
			a.reverse()
		i = a.index(1)
		if [a[(j + i + 1) % n] - a[(j + i) % n] for j in range(n - 1)].count(1) == n - 1:
			res = 'YES'
		else:
			res = 'NO'
	print(res)


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	if len(a) < 3:
		flag = 0
	elif a[0] - a[1] == -1 or a[1] - a[2] == -1:
		for i in range(n - 1):
			if a[i] - a[i + 1] != -1 and a[i] - a[i + 1] != n - 1:
				flag = 1
				break
	else:
		for i in range(n - 1):
			if a[i] - a[i + 1] != 1 and a[i] - a[i + 1] != -(n - 1):
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))

	def solve(p, n):
		index = p.index(1)
		t = p[index:] + p[:index]
		for i in range(n):
			if t[i] != i + 1:
				break
		else:
			return True
		t = p[index + 1:] + p[:index + 1]
		for i in range(n):
			if t[i] != n - i:
				break
		else:
			return True
		return False
	print('YES' if solve(p, n) else 'NO')


q = int(input())
while q > 0:
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	if n > 1:
		if a[0] == 1 and a[1] == n:
			for i in range(1, n - 1):
				if a[i] - a[i + 1] != 1:
					print('NO')
					flag = 1
					break
		elif a[0] == n and a[1] == 1:
			for i in range(1, n - 1):
				if a[i + 1] - a[i] != 1:
					print('NO')
					flag = 1
					break
		elif a[1] > a[0]:
			for i in range(1, n - 1):
				if a[i] != n and a[i + 1] - a[i] != 1:
					print('NO')
					flag = 1
					break
				elif a[i] == n:
					if a[i + 1] != 1:
						print('NO')
						flag = 1
						break
		elif a[1] < a[0]:
			for i in range(1, n - 1):
				if a[i] != 1 and a[i] - a[i + 1] != 1:
					print('NO')
					flag = 1
					break
				elif a[i] == 1:
					if a[i + 1] != n:
						print('NO')
						flag = 1
						break
	if flag == 0:
		print('YES')
	q -= 1


T = int(input())

def ok(b):
	f = 1
	for j in range(len(b)):
		if b[j] != j + 1:
			f = 0
	if f == 1:
		return True
	f = 1
	for k in range(len(b)):
		if b[k] != len(b) - k:
			f = 0
	if f == 1:
		return True
	else:
		return False
for cas in range(T):
	n = int(input())
	a = list(map(int, input().split())) * 2
	flg = 0
	for i in range(n):
		if ok(a[i:i + n]):
			print('YES')
			flg = 1
			break
	if flg == 0:
		print('NO')


n = int(input())
for i in range(n):
	a = []
	b = []
	f = 1
	fq = 4
	nb = int(input())
	b = input().split()
	for j in range(len(b)):
		a.append(int(b[j]))
	for j in range(nb - 1):
		if a[j] - a[j + 1] == 1 or a[j] - a[j + 1] == -1 or a[j] - a[j + 1] == nb - 1 or (nb == 1) or (a[j] - a[j + 1] == -(nb - 1)):
			fq = 1
		else:
			f = 0
	if f == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	for i in range(n):
		if l[i] - l[(i + 1) % n] != 1:
			flag += 1
	if flag == 0 or flag == 1:
		print('YES')
	else:
		flag = 0
		for i in range(n):
			if l[(i + 1) % n] - l[i] != 1:
				flag += 1
		if flag == 0 or flag == 1:
			print('YES')
		else:
			print('NO')


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	for i in range(1, n):
		if abs(arr[i] - arr[i - 1]) != 1:
			if abs(arr[-1] - arr[0]) != 1:
				return 'NO'
			else:
				i += 1
				while i < n:
					if abs(arr[i] - arr[i - 1]) != 1:
						return 'NO'
					i += 1
	return 'YES'
q = int(input())
for _ in range(q):
	print(solve())


def check(temp):
	for i in range(1, len(temp) + 1):
		if i != temp[i - 1]:
			return False
	return True
for z in range(int(input())):
	n = int(input())
	list = [int(i) for i in input().split()]
	ind = list.index(1)
	temp1 = list[ind:] + list[:ind]
	temp2 = list[ind::-1] + list[:ind:-1]
	if check(temp1) or check(temp2):
		print('YES')
	else:
		print('NO')


num_q = int(input())

def red_func(studs, step):
	for i in range(len(studs) - 1):
		if studs[i] == len(studs) and studs[i + step] != 1:
			return False
		elif studs[i] != len(studs) and studs[i + step] != studs[i] + 1:
			return False
	return True
for q in range(num_q):
	num_stud = int(input())
	studs = list(map(int, input().split(' ')))
	cw = red_func(studs, 1)
	ccw = red_func(studs, -1)
	if not (ccw or cw):
		print('NO')
	else:
		print('YES')


def solve():
	n = int(input())
	P = [int(i) for i in input().split()]
	up = True
	for i in range(n - 1):
		if not (P[i] + 1 == P[i + 1] or (P[i] == n and P[i + 1] == 1)):
			up = False
			break
	P.reverse()
	down = True
	for i in range(n - 1):
		if not (P[i] + 1 == P[i + 1] or (P[i] == n and P[i + 1] == 1)):
			down = False
			break
	return up or down
q = int(input())
for _ in range(q):
	if solve():
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	a = a[a.index(1):] + a[:a.index(1)]
	if n < 3:
		print('YES')
	elif a[1] == 2:
		for q in range(2, n):
			if a[q] != q + 1:
				print('NO')
				break
		else:
			print('YES')
	elif a[1] == n:
		for q in range(2, n):
			if a[q] != n - q + 1:
				print('NO')
				break
		else:
			print('YES')
	else:
		print('NO')


requests = input()
for i in range(int(requests)):
	input()
	string = input()
	str_list = string.split()
	int_list = list(map(int, str_list))
	up_sort = sorted(int_list)
	down_sort = sorted(int_list, reverse=True)
	if int_list == up_sort or int_list == down_sort:
		print('YES')
	else:
		min_elem = min(int_list)
		min_elem_index = int_list.index(min_elem)
		list_choice_1 = int_list[min_elem_index:] + int_list[:min_elem_index]
		list_choice_2 = int_list[min_elem_index + 1:] + int_list[:min_elem_index + 1]
		if list_choice_1 == up_sort or list_choice_1 == down_sort:
			print('YES')
		elif list_choice_2 == up_sort or list_choice_2 == down_sort:
			print('YES')
		else:
			print('NO')


k = int(input())
for _ in range(k):
	n = int(input())
	nums = [int(x) for x in input().split()]
	flag1 = True
	start = nums[0]
	for x in nums:
		if x != start:
			flag1 = False
			break
		start = start % n + 1
	flag2 = True
	start = nums[0]
	for x in nums:
		if x != start:
			flag2 = False
			break
		start = (start - 2) % n + 1
	if flag1 or flag2:
		print('YES')
	else:
		print('NO')


import math
q = int(input())
for i in range(q):
	n = int(input())
	arr = input().split()
	for i in range(n):
		arr[i] = int(arr[i])
	if n <= 3:
		print('YES')
	else:
		havent = True
		val = (arr[1] - arr[0]) % n
		if val != 1 and val != n - 1:
			print('NO')
		else:
			for i in range(n - 1):
				if (arr[i + 1] - arr[i]) % n != val and havent:
					print('NO')
					havent = False
			if havent:
				print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 2:
		print('YES')
	else:
		c = True
		uc = True
		for i in range(n):
			if l[(i + 1) % n] - l[i] == 1:
				uc = False
			elif l[(i + 1) % n] - l[i] == -1:
				c = False
			elif l[(i + 1) % n] - l[i] == -(n - 1):
				uc = False
			elif l[(i + 1) % n] - l[i] == n - 1:
				c = False
			else:
				uc = False
				c = False
				break
			if not uc and (not c):
				break
		if uc or c:
			print('YES')
		else:
			print('NO')


def circle(n, lst):
	count = 0
	for i in range(n):
		if abs(lst[i] - lst[(i + 1) % n]) != 1:
			count += 1
	if count == 1 or n <= 2:
		return 'YES'
	return 'NO'
q = int(input())
for j in range(q):
	m = int(input())
	a = [int(x) for x in input().split()]
	print(circle(m, a))


q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	q = p + p + p
	p = sorted(p)
	r = p[::-1]
	f = 0
	for k in range(2 * n):
		if q[k:k + n] == r or q[k:k + n] == p:
			f = 1
			break
	if f == 0:
		print('NO')
	else:
		print('YES')


q = int(input())
for kase in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = [i + 1 for i in range(n)]
	y = x.copy()
	y.reverse()
	ok = False
	for i in range(n):
		if a == x or a == y:
			ok = True
		a.append(a[0])
		a = a[1:]
	if ok:
		print('YES')
	else:
		print('NO')


q = int(input().strip())
for _ in range(q):
	n = int(input().strip())
	nums = [int(i) for i in input().strip().split()]
	nums_rev = list(nums)
	nums_rev.reverse()
	has = True
	has2 = True
	index = -1
	for i in range(n):
		if nums[i] == 1:
			index = i
			break
	temp = nums[index]
	for i in range(index + 1, n):
		if nums[i] < temp:
			has = False
			break
		temp = nums[i]
	for i in range(index):
		if nums[i] < temp:
			has = False
			break
		temp = nums[i]
	index = -1
	for i in range(n):
		if nums_rev[i] == 1:
			index = i
			break
	temp = nums_rev[index]
	for i in range(index + 1, n):
		if nums_rev[i] < temp:
			has2 = False
			break
		temp = nums_rev[i]
	for i in range(index):
		if nums_rev[i] < temp:
			has2 = False
			break
		temp = nums_rev[i]
	if has or has2:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout

def isin(A, B):
	n = len(A)
	return any((A == B[i:i + n] for i in range(len(B) - n + 1)))
n = int(stdin.readline())
q = []
for i in range(n):
	k = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	b = a * 2
	d = list(range(1, k + 1))
	e = d[::-1]
	if isin(d, b):
		q.append('YES')
	elif isin(e, b):
		q.append('YES')
	else:
		q.append('NO')
for i in range(n):
	stdout.write(q[i] + '\n')


q = int(input())
for i in range(q):
	f = True
	n = int(input())
	a = [int(x) for x in input().split()]
	st = 0
	for i in range(n):
		if a[i] == 1:
			st = i
			break
	t = True
	for j in range(1, n):
		m = (st + j - 1) % n
		k = (st + j) % n
		if a[k] - a[m] != 1:
			t = False
			break
	if t:
		print('YES')
		continue
	t = True
	for j in range(1, n):
		m = (st - j + 1) % n
		k = (st - j) % n
		if a[k] - a[m] != 1:
			t = False
			break
	if t:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	input()
	l = list(map(int, input().strip().split()))
	flag = 0
	for i in range(len(l) - 1):
		if not (abs(l[i] - l[i + 1]) == 1 or abs(l[i] - l[i + 1]) == len(l) - 1):
			print('NO')
			flag = 1
			break
	if flag == 0:
		print('YES')


a = int(input())
for i in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	if b > 3:
		for i in range(b - 1):
			if c[i] != 1 and c[i] != b and (c[i + 1] != 1) and (c[i + 1] != b):
				break
		if c[i] < c[i + 1]:
			l = 1
		else:
			l = 2
		f = 1
		c.append(c[0])
		for i in range(b):
			if c[i] > c[i + 1] and l == 1 and (set([c[i], c[i + 1]]) != set([1, b])):
				f = 0
			if c[i] < c[i + 1] and l == 2 and (set([c[i], c[i + 1]]) != set([1, b])):
				f = 0
		if f == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


for i in range(int(input())):
	a = int(input())
	x = [int(x) for x in input().split()]
	x.extend(x)
	c = n = d = 1
	for i in range(len(x) - 1):
		if x[i] < x[i + 1]:
			c += 1
			n = 1
			if c >= a:
				print('YES')
				d = 2
				break
		else:
			n += 1
			c = 1
			if n >= a:
				print('YES')
				d = 2
				break
	if d == 1:
		print('NO')


for t in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = True
	y = True
	for i in range(n - 1):
		if not (a[i] + 1 == a[i + 1] or a[i] - n + 1 == a[i + 1]):
			x = False
			break
	for i in range(n - 1):
		if not (a[i] - 1 == a[i + 1] or a[i] - 1 + n == a[i + 1]):
			y = False
			break
	if x or y:
		print('Yes')
	else:
		print('No')


def horario(x):
	c = 0
	for i in range(len(x)):
		if i + 1 == len(x):
			aux = 0
		else:
			aux = i + 1
		if int(x[i]) == len(x):
			if int(x[aux]) == 1:
				c = c + 1
		elif int(x[i]) + 1 == int(x[aux]):
			c = c + 1
	if c == len(x):
		return True
	else:
		return False

def antihorario(x):
	c = 0
	for i in range(len(x)):
		if i - 1 == -1:
			aux = len(x) - 1
		else:
			aux = i - 1
		if int(x[i]) == len(x):
			if int(x[aux]) == 1:
				c = c + 1
		elif int(x[i]) + 1 == int(x[aux]):
			c = c + 1
	if c == len(x):
		return True
	else:
		return False
n = int(input())
for i in range(n):
	input()
	x = input().split()
	if horario(x) or antihorario(x):
		print('YES')
	else:
		print('NO')


for test in range(int(input())):
	n = int(input())
	k = [int(x) for x in input().split()]
	index = k.index(1)
	if n == 1:
		print('YES')
	else:
		swap = 0
		if k[(index + 1) % n] == 2:
			swap = 1
		elif k[index - 1] == 2:
			swap = -1
		if swap == 0:
			print('NO')
		else:
			check = 1
			flag = False
			for i in range(n):
				if check == k[index % n]:
					check += 1
					index += swap
				else:
					flag = True
					print('NO')
					break
			if not flag:
				print('YES')


noq = int(input())
answers = []
for q in range(noq):
	ccw = 1
	cw = 1
	length = int(input())
	array = list(map(lambda x: int(x), input().split(' ')))
	while array[0] != 1:
		array.append(array.pop(0))
	for i in range(length):
		if array[i] != i + 1:
			cw = 0
	while array[0] != length:
		array.append(array.pop(0))
	for i in range(length):
		if array[i] != length - i:
			ccw = 0
	if ccw or cw:
		answers.append('YES')
	else:
		answers.append('NO')
for i in answers:
	print(i)


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	z = max(l) - min(l)
	flg = 0
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) == 1 or abs(l[i] - l[i + 1]) == z:
			continue
		else:
			flg = 1
			break
	if flg == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = max(l)
	b = min(l)
	a1 = l[l.index(a):n] + l[0:l.index(a)]
	b1 = l[l.index(b):n] + l[0:l.index(b)]
	ans1 = 0
	ans2 = 0
	a1.reverse()
	for i in range(1, len(a1)):
		if a1[i - 1] > a1[i]:
			ans1 = 1
			break
	for j in range(1, len(b1)):
		if b1[j - 1] > b1[j]:
			ans2 = 1
			break
	if ans1 == 0 or ans2 == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ind = arr.index(1)
	new1 = arr[:ind + 1][::-1] + arr[ind + 1:][::-1]
	new2 = arr[ind:] + arr[:ind]
	if new1 == sorted(arr) or new2 == sorted(arr):
		print('YES')
	else:
		print('NO')


nc = int(input())
for case in range(nc):
	ll = int(input())
	ls = list(map(lambda tok: int(tok) - 1, input().split()))
	if all(((ls[i] - ls[i + 1]) % ll == 1 for i in range(-1, ll - 1))):
		print('YES')
	elif all(((ls[i + 1] - ls[i]) % ll == 1 for i in range(-1, ll - 1))):
		print('YES')
	elif ll < 2:
		print('YES')
	else:
		print('NO')


x = int(input())
while x > 0:
	y = int(input())
	stu = [int(x) for x in input().split()]
	flag = 0
	if y == 1 or y == 2 or y == 3:
		print('YES')
	else:
		for i in range(1, y):
			if abs(stu[i] - stu[i - 1]) % y == 1 or abs(stu[i] - stu[i - 1]) % y == y - 1:
				flag = 1
			else:
				flag = 0
				break
		if flag == 1:
			print('YES')
		else:
			print('NO')
	x = x - 1


def get_max(arr):
	maxe = arr[0]
	maxi = 0
	for i in range(1, len(arr)):
		if arr[i] > maxe:
			maxi = i
			maxe = arr[i]
	return maxi

def cclockwise(maxi, arr):
	tam = len(arr)
	for i in range(tam):
		if i != (maxi - 1) % tam and (not arr[i] == arr[(i + 1) % tam] + 1):
			return False
	return True

def clockwise(maxi, arr):
	tam = len(arr)
	for i in range(tam):
		if i != (maxi + 1) % tam and (not arr[i] == arr[(i - 1) % tam] + 1):
			return False
	return True
q = int(input())
for i in range(q):
	s = int(input())
	p = list(map(int, input().split()))
	maxi = get_max(p)
	if cclockwise(maxi, p) or clockwise(maxi, p):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	input()
	n = [int(i) for i in input().split()]
	i = n.index(min(n))
	if n[i - 1] - n[i] == max(n) - min(n):
		x = 1
		i -= 1
		while x < len(n):
			if n[i] - n[i - 1] == 1:
				x += 1
				i -= 1
			else:
				break
		if x == len(n):
			print('YES')
		else:
			print('NO')
	elif n[i - 1] - n[i] == 1:
		x = 1
		i -= 1
		while x < len(n):
			if n[i - 1] - n[i] == 1:
				x += 1
				i -= 1
			else:
				break
		if x == len(n) - 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	min = 10000
	ideal1 = []
	ideal2 = []
	for i in range(n):
		ideal1.append(i + 1)
		ideal2.append(n - i)
	for i in range(n):
		if p[i] < min:
			min = p[i]
			mini = i
	if mini != n - 1:
		if p[mini - 1] - p[mini] > 1:
			s1 = p[0:mini]
			s2 = p[mini:n]
		else:
			s1 = p[0:mini + 1]
			s2 = p[mini + 1:n]
	else:
		s1 = p[0:n - 1]
		s2 = p[n - 1:n]
	if s1 + s2 == ideal1 or s1 + s2 == ideal2 or s2 + s1 == ideal1 or (s2 + s1 == ideal2):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	D = max(arr) - min(arr)
	flag = 1
	for i in range(n - 1):
		diff = abs(arr[i] - arr[i + 1])
		if diff == 1 or diff == D:
			flag = 1
		else:
			flag = 0
			break
	if flag:
		print('YES')
	else:
		print('NO')


q = int(input())
t = []
for i in range(q):
	num = int(input())
	a = [int(p) for p in input().split()]
	if len(a) == 1:
		t.append('YES')
		continue
	b = [0 for i in range(len(a) - 1)]
	for i in range(len(a) - 1):
		b[i] = a[i + 1] - a[i]
	if max(b) == 1:
		for i in range(len(b)):
			if b[i] == 1 or (a[i] == len(a) and a[i + 1] == 1):
				ans = 'YES'
			else:
				ans = 'NO'
				break
		t.append(ans)
	elif max(b) == len(a) - 1 or max(b) == -1:
		for i in range(len(b)):
			if b[i] == -1 or (a[i + 1] == len(a) and a[i] == 1):
				ans = 'YES'
			else:
				ans = 'NO'
				break
		t.append(ans)
	else:
		t.append('NO')
for k in t:
	print(k)


q = int(input())
for i in range(q):
	n = int(input())
	array = list(map(int, input().split()))
	k = array.index(1)
	if k != n - 1:
		if array[k + 1] == n:
			array = array[k + 1:] + array[:k + 1]
		elif array[k - 1] == n:
			array = array[k:] + array[:k]
	elif array != sorted(array, reverse=True):
		array = [1] + array[:k]
	if array == sorted(array) or array == sorted(array, reverse=True):
		print('YES')
	else:
		print('NO')


from sys import stdin
A = int(stdin.readline())
for t in range(0, A):
	B = int(stdin.readline())
	C = list(map(int, stdin.readline().split()))
	S = list()
	if B == 1:
		print('YES')
		continue
	else:
		for t in range(1, B):
			S.append(C[t] - C[t - 1])
		S.append(C[0] - C[B - 1])
	T = set(S)
	TT = list(T)
	TT.sort()
	if len(T) != 2:
		print('NO')
	elif TT[0] == -1 and (TT[1] == B - 1 and S.count(-1) == B - 1):
		print('YES')
	elif TT[0] == -(B - 1) and (TT[1] == 1 and S.count(1) == B - 1):
		print('YES')
	else:
		print('NO')


def cround(a):
	n = len(a)
	t = 0
	for i in range(n - 1):
		if a[i] != n and abs(a[i] - a[i + 1]) == 1:
			continue
		elif a[i] == n and abs(a[i] - a[i + 1]) == n - 1:
			continue
		else:
			t = 1
	if t == 0:
		return True
	else:
		return False

def cround2(a):
	n = len(a)
	t = 0
	for i in range(n - 1):
		if a[i] != 1 and abs(a[i] - a[i + 1]) == 1:
			continue
		elif a[i] == 1 and abs(a[i] - a[i + 1]) == n - 1:
			continue
		else:
			t = 1
	if t == 0:
		return True
	else:
		return False
for i in range(int(input())):
	n = int(input())
	a = [int(j) for j in input().split()]
	if cround(a) or cround2(a):
		print('YES')
	else:
		print('NO')


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_int = lambda : int(sys.stdin.readline())
for _ in range(get_int()):
	n = get_int()
	s = get_list()
	s = s + [s[0]]
	count = 0
	for i in range(n):
		if abs(s[i + 1] - s[i]) > 1:
			count += 1
	if count > 1:
		print('NO')
	else:
		print('YES')


q = int(input())
ans = ''
for i in range(1, q + 1):
	n = int(input())
	L = list(map(int, input().split()))
	b = True
	for k in range(1, n):
		if abs(L[k] - L[k - 1]) != 1 and abs(L[k] - L[k - 1]) != n - 1:
			ans += 'NO\n'
			b = False
			break
	if b:
		ans += 'YES\n'
print(ans.rstrip())


q = int(input())
for i in range(q):
	n = int(input())
	students = list(map(int, input().split()))
	tmp_1 = students[students.index(1):] + students[:students.index(1)]
	tmp_2 = students[students.index(n):] + students[:students.index(n)]
	if tmp_1 == sorted(students) or tmp_2 == sorted(students, reverse=True):
		print('YES')
	else:
		print('NO')


q = int(input())
while q > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	pos = False
	for i in range(n):
		if a[i] == 1:
			s = i
			break
	(cnt, j, prev) = (1, s - 1, 1)
	while cnt < n:
		if j < 0:
			j = n - 1
		if a[j] - prev != 1:
			break
		prev = a[j]
		j -= 1
		cnt += 1
	if cnt == n:
		pos = True
	if not pos:
		(cnt, j, prev) = (1, s + 1, 1)
		while cnt < n:
			if j >= n:
				j = 0
			if a[j] - prev != 1:
				break
			prev = a[j]
			j += 1
			cnt += 1
		if cnt == n:
			pos = True
	print('YES' if pos else 'NO')
	q -= 1


def fun(arr):
	for i in range(len(arr)):
		if arr[i] != i + 1:
			return False
	return True and len(arr) != 0
for t in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	min_ind = arr.index(min(arr))
	a2 = arr[min_ind:] + arr[:min_ind]
	a1 = a2[::-1]
	val = a1.pop(-1)
	a1.insert(0, val)
	if fun(a1) or fun(a2):
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = int(input())
	inp = list(map(int, input().split()))
	inp2 = inp + inp
	res = 0
	s1 = inp.copy()
	s2 = inp.copy()
	s1.sort()
	s2.sort(reverse=True)
	for i in range(n + 1):
		if inp2[i:i + n] == s1 or inp2[i:i + n] == s2:
			res = 1
			break
	if res == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


q = int(input())
for query in range(q):
	n = int(input())
	permutation = list(map(int, input().split()))
	for x in range(n):
		if permutation[x] == 1:
			index = x - 1
			break
	flag = True
	for x in range(1, n):
		if permutation[(x + index) % n] != x:
			flag = False
			break
	index = index + 2
	if flag == False:
		flag = True
		for x in range(1, n):
			if permutation[(index - x) % n] != x:
				flag = False
				break
	print('YES' if flag else 'NO')


q = int(input())
for i in range(q):
	flag = True
	n = int(input())
	arr = list(map(int, input().split()))
	lst = arr[::-1]
	for j in range(n):
		if arr[j:] == list(range(1, n + 1)) or lst[j:] == list(range(1, n + 1)):
			print('YES')
			flag = False
			break
		arr.append(arr[j])
		lst.append(lst[j])
	if flag:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	ind = l.index(1)
	c1 = l[ind:] + l[:ind]
	c2 = l[ind + 1:] + l[:ind + 1]
	l.sort()
	if l == c1 or l == c2:
		print('YES')
		continue
	l.reverse()
	if l == c1 or l == c2:
		print('YES')
		continue
	print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	(p, o) = ([], [])
	for j in range(n - 1):
		p.append(arr[j + 1] - arr[j])
		o.append(arr[j] - arr[j + 1])
	p.append(arr[0] - arr[-1])
	o.append(arr[-1] - arr[0])
	if o.count(1) >= n - 1 or p.count(1) >= n - 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(c1, c2) = (0, 0)
	for i in range(n):
		if l[i] > l[i - 1]:
			c1 += 1
		else:
			c2 += 1
	if c1 < 2 or c2 < 2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split())) * 3
	m = p[n:].index(1) + n
	if p[m:m + n] == list(range(1, n + 1)):
		print('YES')
	else:
		p.reverse()
		m = p[n:].index(1) + n
		if p[m:m + n] == list(range(1, n + 1)):
			print('YES')
		else:
			print('NO')


def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		a = list(map(int, input().split()))
		finalans = False
		for i in range(n):
			got = True
			for j in range(n):
				if a[(i + j + 1) % n] != a[(i + j) % n] + 1 and (not (a[(i + j + 1) % n] == 1 and a[(i + j) % n] == n)):
					got = False
					break
			if got:
				finalans = True
				print('YES')
				break
		if finalans:
			continue
		for i in range(n):
			got = True
			for j in range(n):
				if a[(i + j + 1) % n] != a[(i + j) % n] - 1 and (not (a[(i + j + 1) % n] == n and a[(i + j) % n] == 1)):
					got = False
					break
			if got:
				finalans = True
				print('YES')
				break
		if finalans:
			continue
		print('NO')
	return
main()


def eval(list_pos):
	indexStart = list_pos.index(1)
	for k in range(len(list_pos) - 1):
		if list_pos[(indexStart + k) % len(list_pos)] - list_pos[(indexStart + k + 1) % len(list_pos)] != -1:
			return 0
	return 1

def evalReverse(list_pos):
	indexStart = list_pos.index(1)
	for k in range(len(list_pos) - 1):
		if list_pos[(indexStart - k) % len(list_pos)] - list_pos[(indexStart - k - 1) % len(list_pos)] != -1:
			return 0
	return 1
q = int(input())
for query in range(q):
	n = int(input())
	pos = list(map(int, input().split()))
	if eval(pos) or evalReverse(pos):
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	x = len(a)
	ind = a.index(1)
	t = 1
	tag = True
	for i in range(ind, x):
		if a[i] == t:
			t = t + 1
		else:
			tag = False
	if tag:
		t = x
		for i in range(ind - 1, -1, -1):
			if t == a[i]:
				t = t - 1
			else:
				tag = False
	ind = a.index(1)
	t = 1
	tag1 = True
	for i in range(ind, -1, -1):
		if a[i] == t:
			t = t + 1
		else:
			tag1 = False
	if tag1:
		t = x
		for i in range(ind + 1, x):
			if t == a[i]:
				t = t - 1
			else:
				tag1 = False
	if tag or tag1:
		print('YES')
	else:
		print('NO')


q = int(input())
for z in range(0, q):
	n = int(input())
	l = [int(i) for i in input().split()]
	maxDiff = max(l) - min(l)
	flag = False
	side = -1
	for i in range(0, n - 1):
		if l[i] == l[i + 1] + 1:
			if side == 1:
				print('NO')
				flag = True
				break
			else:
				side = 0
		elif l[i] == l[i + 1] - 1:
			if side == 0:
				print('NO')
				flag = True
				break
			else:
				side = 1
		elif abs(l[i] - l[i + 1]) != maxDiff:
			print('NO')
			flag = True
			break
		else:
			pass
	if not flag:
		print('YES')


n = int(input())
for _ in range(0, n):
	x = int(input())
	a = list(map(int, input().split()))
	flag = True
	for i in range(0, x - 1):
		if flag == False:
			break
		if abs(a[i] - a[i + 1]) == 1 or abs(a[i] - a[i + 1]) == x - 1:
			continue
		else:
			flag = False
	if flag:
		print('YES')
	else:
		print('NO')


import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
ORDA = 97

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return [int(i) for i in input().split()]

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=2):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	new_number = 0
	while number > 0:
		new_number += number % base
		number //= base
	return new_number

def cdiv(n, k):
	return n // k + (n % k != 0)

def ispal(s):
	for i in range(len(s) // 2 + 1):
		if s[i] != s[-i - 1]:
			return False
	return True
for _ in range(ii()):
	n = ii()
	a = li()
	pos1 = a.index(1)
	fsample = [i for i in range(1, n + 1)]
	bsample = [i for i in range(n, 0, -1)]
	f = a[pos1:] + a[:pos1]
	b = a[pos1 + 1:] + a[:pos1 + 1]
	if f == fsample or b == bsample:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	done = False
	x = int(input())
	a = [int(a) for a in input().split()[:x]]
	if not done:
		indexMin = a.index(min(a))
		k = indexMin
		for j in range(x):
			if k < len(a) - 1:
				if a[k + 1] - a[k] != 1:
					break
			elif k == len(a) - 1 and j < x - 1:
				if a[0] - a[k] != 1:
					break
				else:
					k = -1
			k += 1
			if k + 1 == indexMin or j == x - 1:
				print('YES')
				done = True
	if not done:
		indexMax = a.index(max(a))
		k = indexMax
		for j in range(x):
			if k < len(a) - 1:
				if a[k] - a[k + 1] != 1:
					break
			elif k == len(a) - 1 and j < x - 1:
				if a[k] - a[0] != 1:
					break
				else:
					k = -1
			k += 1
			if k + 1 == indexMax or j == x - 1:
				print('YES')
				done = True
	if not done:
		print('NO')


T = int(input())
for _ in range(T):
	str = 'YES'
	N = int(input())
	P = list(map(int, input().split()))
	i = P.index(1)
	for x in range(N):
		if i == N:
			i = 0
		if P[i] is not x + 1:
			str = 'NO'
		i += 1
	i = P.index(1)
	if str == 'NO':
		str = 'YES'
		for x in range(N):
			if i == -1:
				i = N - 1
			if P[i] is not x + 1:
				str = 'NO'
			i -= 1
	print(str)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = a.index(1)
	(f1, f2) = (True, True)
	for i in range(n - 1):
		if a[(k + i) % n] > a[(k + i + 1) % n]:
			f1 = False
			break
	for i in range(n - 1):
		if a[(k - i) % n] > a[(k - i - 1) % n]:
			f2 = False
			break
	print('YES' if f1 or f2 else 'NO')


for q in range(int(input())):
	n = int(input())
	a = [int(x) - 1 for x in input().split()]
	b1 = all(((l + 1) % n == r for (l, r) in zip(a[:-1], a[1:])))
	b2 = all(((l + 1) % n == r for (r, l) in zip(a[:-1], a[1:])))
	print('YES') if b1 or b2 else print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x = sorted(a)
	y = x[::-1]
	for i in range(n + 1):
		if a[i:] + a[:i] in (x, y):
			print('YES')
			break
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	array = list(map(int, input().split()))
	cw = 0
	ccw = 0
	for i in range(len(array)):
		if array[i] < array[i - 1]:
			cw += 1
		else:
			ccw += 1
	if cw <= 1 or ccw <= 1:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	p = [int(j) for j in input().split()] * 2
	k = 0
	for j in range(2 * n - 1):
		d = abs(p[j + 1] - p[j])
		k = k + 1 if d == 1 else 0
		if k == n - 1:
			k = -1
			break
	print('YES' if k == -1 else 'NO')


def isChorovod(lst):
	if len(lst) <= 2:
		return True
	direction = lst[1] - lst[0]
	if direction == len(lst) - 1:
		direction = -1
	elif direction == 1 - len(lst):
		direction = 1
	if direction != 1 and direction != -1:
		return False
	for k in range(2, len(lst)):
		if lst[k] != (lst[k - 1] + direction - 1) % len(lst) + 1:
			return False
	return True
q = int(input())
for _ in range(q):
	c = int(input())
	arr = list(map(int, input().split()))
	assert c == len(arr)
	isch = isChorovod(arr)
	print('YES' if isch else 'NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	min_ind = arr.index(1)
	count = n - 1
	flag = 0
	val = 2
	min_ind = (min_ind + 1) % n
	while count:
		count -= 1
		if val != arr[min_ind]:
			flag = 1
			break
		val += 1
		min_ind = (min_ind + 1) % n
	if flag == 0:
		print('YES')
	else:
		max_ind = arr.index(n)
		count = n - 1
		flag = 0
		val = n - 1
		max_ind = (max_ind + 1) % n
		while count:
			count -= 1
			if val != arr[max_ind]:
				flag = 1
				break
			val -= 1
			max_ind = (max_ind + 1) % n
		if flag == 0:
			print('YES')
		else:
			print('NO')


t = int(input())
for iii in range(t):
	l = int(input())
	k = list(map(int, input().split()))
	k = k + k
	ans = True
	for i in range(l):
		ans = True
		for j in range(i + 1, i + l):
			if abs(k[j] - k[j - 1]) > 1:
				ans = False
				break
		if ans:
			print('YES')
			break
		ans = False
	if ans == False:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	data = [int(i) for i in input().split()]
	f = data.index(1) - n
	good = True
	if 2 == data[f + 1]:
		for i in range(n):
			if data[f + i] != i + 1:
				good = False
				break
	else:
		f += n
		for i in range(n):
			if data[f - i] != i + 1:
				good = False
				break
	if good:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ind = arr.index(1)
	count = 1
	flag = 1
	for i in range(ind + 1, n):
		if arr[i] == count + 1:
			count += 1
		else:
			flag = 0
	for i in range(0, ind):
		if arr[i] == count + 1:
			count += 1
		else:
			flag = 0
	count2 = 1
	flag2 = 1
	for i in range(ind - 1, -1, -1):
		if arr[i] == count2 + 1:
			count2 += 1
		else:
			flag2 = 0
	for i in range(n - 1, ind, -1):
		if arr[i] == count2 + 1:
			count2 += 1
		else:
			flag2 = 0
	if flag or flag2:
		print('YES')
	else:
		print('NO')


from sys import stdin
q = int(input())
for i in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	once = False
	cannot = False
	for j in range(1, n):
		if abs(p[j] - p[j - 1]) > 1:
			if once:
				cannot = True
				break
			else:
				once = True
	if abs(p[0] - p[n - 1]) > 1:
		if once:
			cannot = True
		else:
			once = True
	if cannot:
		print('NO')
	else:
		print('YES')


for q in range(int(input())):
	n = int(int(input()))
	a = list(map(int, input().split(' ')))
	ans = 'YES'
	for i in range(1, n):
		if not (a[i - 1] == n and a[i] == 1 or a[i] == a[i - 1] + 1):
			ans = 'NO'
	a.reverse()
	rans = 'YES'
	for i in range(1, n):
		if not (a[i - 1] == n and a[i] == 1 or a[i] == a[i - 1] + 1):
			rans = 'NO'
	if rans == 'NO' and ans == 'NO':
		print(rans)
	else:
		print('YES')


for q in range(int(input())):
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	start = arr.index(1)
	res = True
	for i in range(n - 1):
		if not arr[(start + i) % n] + 1 == arr[(start + i + 1) % n]:
			res = False
			break
	if res:
		print('YES')
		continue
	else:
		res = True
		for i in range(n - 1):
			if not arr[(start - i) % n] + 1 == arr[(start - i - 1) % n]:
				res = False
				break
		if res:
			print('YES')
			continue
	print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	LIST = [int(s) for s in input().split()]
	s = LIST.index(1)
	check1 = True
	check2 = True
	for j in range(s, n - 1):
		if LIST[j + 1] - LIST[j] != 1:
			check1 = False
			break
	for j in range(1, s):
		if LIST[j] - LIST[j - 1] != 1:
			check1 = False
			break
	for x in range(s, 0, -1):
		if LIST[x - 1] - LIST[x] != 1:
			check2 = False
			break
	for x in range(n - 1, s + 1, -1):
		if LIST[x] - LIST[x - 1] != -1:
			check2 = False
			break
	if check1 == True or check2 == True:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	for i in range(n):
		if l[i] == 1:
			a = i
			break
	ok1 = True
	ok2 = True
	for j in range(n):
		ok1 = ok1 and l[(a + n - j) % n] == j + 1
		ok2 = ok2 and l[(a + n + j) % n] == j + 1
	if ok1 or ok2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 3:
		print('YES')
		continue
	ind = a.index(1)
	if a[ind - 1] == n:
		if ind == n - 1:
			na = [a[-1]] + a[:n - 1]
		else:
			na = a[ind:] + a[:ind]
	elif a[(ind + 1) % n] == n:
		if ind == 0:
			na = [a[0]] + a[-1:0:-1]
		else:
			na = a[:ind + 1][::-1] + a[-1:ind:-1]
	else:
		print('NO')
		continue
	c = [i for i in range(1, n + 1)]
	if na != c:
		print('NO')
	else:
		print('YES')


num = int(input())
for i in range(num):
	n = int(input())
	students = []
	min_index = 0
	max_index = 0
	is_YES = True
	tmp = input().split()
	for i in range(n):
		students.append(int(tmp[i]))
		if students[i] == 1:
			min_index = i
		elif students[i] == n:
			max_index = i
	if (min_index + 1) % n == max_index:
		for i in range(n):
			if students[(min_index + n - i) % n] != i + 1:
				is_YES = False
	elif (min_index - 1) % n == max_index:
		for i in range(n):
			if students[(min_index + n + i) % n] != i + 1:
				is_YES = False
	else:
		is_YES = False
	if is_YES:
		print('YES')
	else:
		print('NO')


from sys import stdin as si

class Solution:

	def __init__(self):
		pass

	def result(self, A):
		l = len(A)
		for i in range(1, l):
			if abs(A[i] - A[i - 1]) == 1:
				continue
			elif abs(A[i] - A[i - 1]) != l - 1:
				return 'NO'
		return 'YES'
for i in range(int(si.readline().strip())):
	S = Solution()
	n = si.readline().strip()
	lst = list(map(int, si.readline().strip().split()))
	print(S.result(lst))


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	x = p.index(n)
	l = p[x:]
	l += p[:x]
	f = True
	for i in range(n):
		if l[i] != n - i:
			f = False
	if f:
		print('YES')
		continue
	xx = p.index(1)
	ll = p[xx:]
	ll += p[:xx]
	ff = True
	for i in range(n):
		if ll[i] != i + 1:
			ff = False
	if ff:
		print('YES')
		continue
	print('NO')


for _ in range(int(input())):
	n = int(input())
	p = [int(x) - 1 for x in input().split()]
	if all(map(lambda i: (p[i] + 1) % n == p[(i + 1) % n], range(n))) or all(map(lambda i: p[i] == (p[(i + 1) % n] + 1) % n, range(n))):
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	f = True
	a = [int(x) for x in input().split()]
	a.append(a[0])
	for i in range(1, n):
		if abs(a[i] - a[i - 1]) != 1 and abs(a[i] - a[i - 1]) != n - 1:
			f = False
			break
	print('YES' if f else 'NO')


T = int(input())
for i in range(T):
	N = int(input())
	Ps = list(map(int, input().split()))
	Ns = list(range(N))
	i = 0
	for j in range(N):
		Ns[Ps[j] - 1] = j
	cansun = 2
	canni = 2
	for i in range(N):
		if i < N - 1 and Ns[i] != Ns[i + 1] - 1:
			cansun -= 1
		if i < N - 1 and Ns[i] != Ns[i + 1] + 1:
			canni -= 1
		if i == N - 1:
			if Ns[i] != Ns[0] - 1:
				cansun -= 1
			if Ns[i] != Ns[0] + 1:
				canni -= 1
	if canni == 1 or cansun == 1:
		print('YES')
	else:
		print('NO')


quantidade = int(input())
for k in range(quantidade):
	qntdalunos = int(input())
	lista = [int(x) for x in input().split()]
	flaghorario = True
	flagantihorario = True
	for i in range(qntdalunos):
		if lista[i] == qntdalunos and i != qntdalunos - 1:
			if lista[i + 1] != lista[i] - 1:
				flagantihorario = False
			if lista[i + 1] != 1:
				flaghorario = False
		elif lista[i] == 1 and i != qntdalunos - 1:
			if lista[i + 1] != qntdalunos:
				flagantihorario = False
			if lista[i + 1] != lista[i] + 1:
				flaghorario = False
		elif i != qntdalunos - 1:
			if lista[i + 1] != lista[i] - 1:
				flagantihorario = False
			if lista[i + 1] != lista[i] + 1:
				flaghorario = False
		if not flaghorario and (not flagantihorario):
			break
	if flaghorario or flagantihorario:
		print('YES')
	else:
		print('NO')


t = int(input())
for u in range(0, t):
	n = int(input())
	a = []
	s = input()
	for i in s.split():
		a.append(int(i))
	f = 1
	for i in range(0, n):
		if a[i] == 1:
			j = i
			for c in range(1, n + 1):
				if c != a[j]:
					f = 0
					break
				j = (j + 1) % n
			if f == 1:
				break
			f = 1
			j = i
			for c in range(1, n + 1):
				if c != a[j]:
					f = 0
					break
				j = (j - 1 + n) % n
			break
	if f:
		print('YES')
	else:
		print('NO')


def Checker(ender):
	global flag
	for i in range(ender):
		if students[i] + 1 != students[i + 1]:
			flag = 1
			break
zapros = int(input())
flag = 0
for q in range(zapros):
	kolvo = int(input())
	students = [int(i) for i in input().split()]
	if kolvo >= 3:
		if students[0] - 1 == students[1] or students[1] - 1 == students[2]:
			students.reverse()
		start = students.index(1)
		if start == 0:
			Checker(kolvo - 1)
		elif start == kolvo - 1:
			Checker(kolvo - 2)
			if students[-2] != kolvo:
				flag = 1
		else:
			for i in range(start, kolvo - 1):
				if students[i] + 1 != students[i + 1]:
					flag = 1
					break
			Checker(start - 2)
			if students[start - 1] != kolvo:
				flag = 1
	elif kolvo == 2:
		if students[0] + 1 != students[1] and students[0] - 1 != students[1]:
			flag = 1
	if flag == 0:
		print('YES')
	else:
		print('NO')
	flag = 0


def is_doable(n, p):
	ind_1 = p.index(1)
	dir_check = list(range(1, n + 1))
	direct = []
	reverse = []
	for i in range(ind_1, n):
		direct.append(p[i])
	for i in range(ind_1):
		direct.append(p[i])
	for i in reversed(range(ind_1 + 1)):
		reverse.append(p[i])
	for i in reversed(range(ind_1 + 1, n)):
		reverse.append(p[i])
	if direct == dir_check or reverse == dir_check:
		return 'YES'
	else:
		return 'NO'
q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	print(is_doable(n, p))


def isClockwise(res):
	start = 0
	for i in range(0, len(res)):
		if res[i] == 1:
			start = i
			break
	newL = []
	for i in range(start, len(res)):
		newL.append(res[i])
	for i in range(0, start):
		newL.append(res[i])
	for i in range(1, len(newL)):
		if newL[i] < newL[i - 1]:
			return False
	return True

def isCounterClockwise(res):
	start = 0
	for i in range(0, len(res)):
		if res[i] == 1:
			start = i
			break
	newL = []
	for i in range(start, -1, -1):
		newL.append(res[i])
	for i in range(len(res) - 1, start, -1):
		newL.append(res[i])
	for i in range(1, len(newL)):
		if newL[i] < newL[i - 1]:
			return False
	return True

def main():
	q = int(input())
	for i in range(0, q):
		n = int(input())
		l = input()
		l = l.split(' ')
		res = list(map(lambda x: int(x), l))
		if isClockwise(res) or isCounterClockwise(res):
			print('YES')
		else:
			print('NO')
main()


q = int(input())
for l in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	index1 = arr.index(1)
	arr1 = []
	for i in range(index1, -1, -1):
		arr1.append(arr[i])
	for i in range(n - 1, index1, -1):
		arr1.append(arr[i])
	arr2 = []
	for i in range(index1, n):
		arr2.append(arr[i])
	for i in range(0, index1):
		arr2.append(arr[i])
	flag = 0
	for i in range(n - 1):
		if arr1[i] + 1 != arr1[i + 1]:
			flag += 1
			break
	for i in range(n - 1):
		if arr2[i] + 1 != arr2[i + 1]:
			flag += 1
			break
	if flag == 2:
		print('NO')
	else:
		print('YES')


x = int(input())
z = 0
f = []
while z < x:
	l = []
	n = int(input())
	q = n
	for n in map(int, input().split()):
		l.append(n)
	h = 0
	if q == 1:
		f.append('YES')
		z += 1
		continue
	while h < len(l) - 1:
		if abs(l[h] - l[h + 1]) > 1:
			if l[h] == min(l) and l[h + 1] == max(l):
				dj = 0
			elif l[h] == max(l) and l[h + 1] == min(l):
				dj = 0
			else:
				f.append('NO')
				break
		h += 1
		if h == len(l) - 1:
			f.append('YES')
	z += 1
vf = 0
while vf < len(f):
	print(f[vf])
	vf += 1


from bisect import bisect_left
Q = int(input())
data = []
for _ in range(Q):
	N = int(input())
	P = list(map(int, input().split()))
	data.append([N, P])
for (N, P) in data:
	lok = True
	for (i, n) in enumerate(P):
		if i == 0:
			pre = n
			if n == N:
				pre = 0
			continue
		if n == N:
			pre = 0
			continue
		if pre + 1 != n:
			lok = False
			break
		pre = n
	rok = True
	for (i, n) in enumerate(P):
		if i == 0:
			pre = n
			if n == 1:
				pre = N + 1
			continue
		if n == 1:
			pre = N + 1
			continue
		if pre - 1 != n:
			rok = False
			break
		pre = n
	if lok or rok:
		print('YES')
	else:
		print('NO')


tmp = input()
q = int(tmp)
for i in range(q):
	n = int(input())
	tmp = input().split()
	clock = 0
	isOK = True
	before = -1
	for j in range(n):
		t = int(tmp[j])
		if before == -1:
			before = t
			continue
		if clock == 0:
			if (t - before + n) % n == 1:
				clock = 1
			elif (t - before + n) % n == n - 1:
				clcok = n - 1
			else:
				isOK = False
				break
		elif (t - before + n) % n != clock:
			isOK = False
			break
		before = t
	if isOK:
		print('YES')
	else:
		print('NO')


def solve(a, n):
	prevdiff = None
	for i in range(n - 1):
		diff = a[i + 1] - a[i]
		if prevdiff is None:
			prevdiff = diff
		elif prevdiff in (1, 1 - n):
			if diff == 1 or diff == 1 - n:
				continue
			else:
				print('NO')
				break
		elif prevdiff in (-1, n - 1):
			if diff == -1 or diff == n - 1:
				continue
			else:
				print('NO')
				break
		else:
			print('NO')
			break
	else:
		print('YES')
q = int(input())
for _ in range(q):
	n = int(input())
	a = [int(i) for i in input().split()]
	solve(a, n)


T = int(input())
while T:
	T -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		if a[0] == 1:
			print('YES')
		else:
			print('NO')
		continue
	pos = -1
	for i in range(n):
		if a[i] == 1:
			pos = i
			break
	if pos == -1:
		print('NO')
		continue
	elif pos == n - 1:
		if a[0] == 2:
			right = True
		elif a[pos - 1] == 2:
			right = False
		else:
			print('NO')
			continue
	elif a[pos + 1] == 2:
		right = True
	elif a[pos - 1] == 2:
		right = False
	else:
		print('NO')
		continue
	if right:
		diff = -1
	else:
		diff = 1
	failed = False
	once = False
	for i in range(1, n):
		if a[i - 1] - a[i] != diff:
			if not once and abs(a[i - 1] - a[i]) == n - 1:
				once = True
			else:
				print('NO')
				failed = True
				break
	if not failed:
		print('YES')


x = int(input())
listofanswer = list(range(x))
for i in range(x):
	n = int(input())
	q = [int(x1) for x1 in input().split(' ')]
	l = q.index(1)
	l1 = l
	q1 = list(range(n))
	q3 = list(range(n))
	q2 = sorted(q)
	q4 = sorted(q2, key=int, reverse=True)
	for j in range(n):
		q1[j] = q[l]
		q3[j] = q[l1]
		l1 = l1 - 1
		if l1 < 0:
			l1 = n - 1
		l = l + 1
		if l >= n:
			l = 0
	if q2 == q1 or q4 == q3 or q2 == q3 or (q4 == q1):
		listofanswer[i] = 0
	else:
		listofanswer[i] = 1
for i in range(x):
	if listofanswer[i] == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for v in range(t):
	n = int(input())
	x = [int(q) for q in input().split()]
	i = x.index(1)
	a1 = x[i:] + x[:i]
	a2 = x[i + 1:] + x[:i + 1]
	a2.reverse()
	y = x
	y = sorted(y)
	if y == a1 or y == a2:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	p = [*map(int, input().split())]
	back = None
	dance1 = True
	for j in range(n):
		next = (j + 1) % n
		if p[next] != p[j] + 1 and p[next] != 1:
			dance1 = False
			break
	dance2 = True
	for j in range(n):
		next = (j - 1) % n
		if p[next] != p[j] + 1 and p[next] != 1:
			dance2 = False
			break
	if dance1 or dance2:
		print('YES')
	else:
		print('NO')


for q in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	x1 = []
	x1.append(abs(a[0] - a[-1]))
	x2 = []
	x2.append(abs(a[0] - a[-1]))
	for i in range(1, n):
		x1.append(abs(a[i] - a[i - 1]))
	for i in range(n - 1, 0, -1):
		x2.append(abs(a[i] - a[i - 1]))
	c1 = 0
	c2 = 0
	for i in range(len(x1)):
		if x1[i] > 1:
			c1 += 1
		if x2[i] > 1:
			c2 += 1
	if c1 > 1 and c2 > 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for iteration in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	can_dance = True
	if n > 2:
		d = (a[1] - a[0] + n) % n
		if d != 1 and d != n - 1:
			can_dance = False
		for i in range(n - 1):
			if (a[i + 1] - a[i] - d + 2 * n) % n != 0:
				can_dance = False
	if can_dance:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	if n < 3:
		print('YES')
	else:
		dif1 = p[1] - p[0]
		dif2 = p[2] - p[1]
		if dif1 in [-1, 1]:
			for i in range(n - 1):
				if dif1 == 1:
					if p[i + 1] - p[i] != dif1 and p[i + 1] != 1:
						print('NO')
						break
				elif p[i + 1] - p[i] != dif1 and p[i] != 1:
					print('NO')
					break
			else:
				print('YES')
		elif dif2 in [-1, 1]:
			for i in range(1, n - 1):
				if p[i + 1] - p[i] != dif2:
					print('NO')
					break
			else:
				print('YES')
		else:
			print('NO')


number_queries = int(input())
queries = []
results = []
for num in range(number_queries):
	number_students = int(input())
	students = [int(x) for x in input().split()]
	queries.append([number_students, students])
for num in queries:
	if num[0] == 1:
		results.append('YES')
	else:
		for hor in range(len(num[1]) - 1):
			if hor == len(num[1]) - 2:
				results.append('YES')
				break
			r = num[1][hor] - num[1][hor + 1]
			if abs(r) == 1 or abs(r) == len(num[1]) - 1:
				continue
			else:
				results.append('NO')
				break
print('\n'.join(results))


n = int(input())
for i in range(n):
	num = int(input())
	listaNum = list(map(int, input().split()))
	indiceUno = listaNum.index(1)
	ans = 'YES'
	if len(listaNum) == 1 or len(listaNum) == 2 or len(listaNum) == 3:
		ans = 'YES'
	elif listaNum[(indiceUno + 1) % len(listaNum)] == 2:
		for i in range(len(listaNum) - 1):
			if abs(listaNum[(indiceUno + i) % len(listaNum)] - listaNum[(indiceUno + i + 1) % len(listaNum)]) != 1:
				ans = 'NO'
				break
	elif listaNum[indiceUno - 1 % len(listaNum)] == 2:
		for i in range(len(listaNum) - 1):
			if abs(listaNum[(indiceUno - i) % len(listaNum)] - listaNum[(indiceUno - i - 1) % len(listaNum)]) != 1:
				ans = 'NO'
				break
	else:
		ans = 'NO'
	print(ans)


test = int(input())
for j in range(test):
	size = int(input())
	lst = [int(x) for x in input().split()]
	if size == 1:
		print('YES')
	else:
		flag = True
		for i in range(1, size):
			ff = abs(lst[i] - lst[i - 1])
			if ff != 1 and ff != size - 1:
				flag = False
				break
		if flag:
			print('YES')
		else:
			print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	f = 0
	for j in range(n):
		if abs(a[j] - a[j - 1]) > 1:
			f += 1
	if f < 2:
		print('YES')
	else:
		print('NO')


for q in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	a = list(range(1, n + 1))
	b = a[::-1]
	flag = True
	for i in range(n):
		new_arr1 = arr[i:] + arr[:i]
		new_arr2 = arr[:i][::-1] + arr[i:][::-1]
		if new_arr1 == a or new_arr2 == a:
			print('YES')
			flag = False
			break
	if flag:
		print('NO')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	cnt = 0
	for i in range(n):
		if abs(l[i] - l[i - 1]) > 1:
			cnt += 1
	print('YES' if cnt < 2 else 'NO')


def check(n, list1):
	yes = 1
	for j in range(n - 1):
		if list1[j + 1] - list1[j] != 1 and list1[j] - list1[j + 1] != n - 1:
			list1.reverse()
			for j in range(n - 1):
				if list1[j + 1] - list1[j] != 1 and list1[j] - list1[j + 1] != n - 1:
					return 'NO'
	return 'YES'
q = int(input())
for i in range(q):
	n = int(input())
	list1 = list(map(int, input().split(' ')))
	if n == 1:
		print('YES')
	else:
		print(check(n, list1))


def cyclic_equiv(u, v):
	(n, i, j) = (len(u), 0, 0)
	while i < n and j < n:
		k = 1
		while k <= n and u[(i + k) % n] == v[(j + k) % n]:
			k += 1
		if k > n:
			return True
		if u[(i + k) % n] > v[(j + k) % n]:
			i += k
		else:
			j += k
	return False

def cyclic_equiv_anti(u, v):
	(n, i, j) = (len(u), 0, 0)
	while i < n and j < n:
		k = 1
		while k <= n and u[(i + k) % n] == v[(j + k) % n]:
			k += 1
		if k > n:
			return True
		if u[(i + k) % n] > v[(j + k) % n]:
			i += k
		else:
			j += k
	return False

def is_circular_sorted(arr):
	count = 0
	length = len(arr)
	for i in range(length):
		if arr[i] > arr[(i + 1) % len(arr)]:
			count += 1
	return count <= 1

def is_circular_sorted_anti(arr):
	count = len(arr)
	length = len(arr)
	for i in range(length):
		if arr[i] > arr[(i + 1) % len(arr)]:
			count -= 1
	return count <= 1
q = int(input())
for te in range(0, q):
	n = int(input())
	l = [int(x) for x in input().split()]
	p = []
	for i in range(1, n + 1):
		p.append(i)
	if is_circular_sorted(l) or is_circular_sorted_anti(l):
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	w = int(input())
	e = list(map(int, input().split()))
	m = e.index(min(e))
	t = True
	y = True
	for j in range(m, m + w - 1):
		if e[j % w] + 1 != e[(j + 1) % w]:
			t = False
	for j in range(m, m - w + 1, -1):
		if e[j % w] + 1 != e[(j - 1) % w]:
			y = False
	if t or y:
		print('YES')
	else:
		print('NO')


q = int(input())
B = []
for m in range(q):
	n = int(input())
	s = map(int, input().split())
	a = list(s)
	ind = 0
	for i in range(n):
		if a[i] == 1:
			ind = i
			break
	lo = []
	for i in range(n):
		lo.append(a[i])
	for i in range(n):
		lo.append(a[i])
	p = True
	o = True
	for i in range(ind + 1, ind + n):
		if lo[i] < lo[i - 1]:
			p = False
	for i in range(ind + n - 1, ind, -1):
		if lo[i] < lo[i + 1]:
			o = False
	if p or o:
		B.append('YES')
	else:
		B.append('NO')
for i in range(q):
	print(B[i])


qs = int(input())
for q in range(qs):
	n = int(input())
	stud = [int(elem) - 1 for elem in input().split()]
	forward = True
	backward = True
	first = True
	for s in stud:
		if first:
			last = s
			first = False
		else:
			if forward and (not (last + 1) % n == s):
				forward = False
			if backward and (not (last - 1) % n == s):
				backward = False
		last = s
	if forward or backward:
		print('YES')
	else:
		print('NO')


q = int(input())
while q:
	n = int(input())
	a = input().split()
	for i in range(n):
		a[i] = int(a[i])
	b = 0
	c = 0
	for j in range(n):
		if a[j] == 1:
			b = j
		elif a[j] == n:
			c = j
	di = []
	ei = []
	for k in range(b, b + n):
		di.append(a[k % n])
	for l in range(c, c + n):
		ei.append(a[l % n])
	d = [i + 1 for i in range(n)]
	e = [n - i for i in range(n)]
	if di == d or ei == e:
		print('YES')
	else:
		print('NO')
	q -= 1


from sys import stdin
input = stdin.readline

def ckw():
	res = True
	i = 0
	while res and i < n - 1:
		if p[(pos_first + i) % n] > p[(pos_first + (i + 1)) % n]:
			res = False
		i += 1
	return res

def ctw():
	res = True
	i = 0
	while res and i < n - 1:
		if p[(pos_first - i) % n] > p[(pos_first - (i + 1)) % n]:
			res = False
		i += 1
	return res
q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(i) for i in input().split()]
	pos_first = p.index(1)
	if ckw() or ctw():
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
import math
T = int(input())
for i in range(T):
	N = int(input())
	arr = [int(x) for x in stdin.readline().split()]
	if N <= 2:
		print('YES')
	else:
		idx = -1
		avail = 2
		for j in range(N):
			if arr[j] == 1:
				idx = j
				break
		k = 1
		for j in range(idx, idx + N):
			idd = j % N
			if arr[idd] != k:
				avail -= 1
				break
			k += 1
		k = 1
		for j in range(idx, idx - N, -1):
			idd = j % N
			if arr[idd] != k:
				avail -= 1
				break
			k += 1
		if avail > 0:
			print('YES')
		else:
			print('NO')


from sys import stdin
input = stdin.readline
for i in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	s = p.index(n)
	t = False
	for i in range(s, n + s - 1):
		if i >= n - 1:
			i -= n
		if p[i] - p[i + 1] == 1:
			continue
		else:
			break
	else:
		t = True
	for i in range(s, s - n + 1, -1):
		if p[i] - p[i - 1] == 1:
			continue
		else:
			break
	else:
		t = True
	if t:
		print('YES')
	else:
		print('NO')


num_q = int(input())
for i in range(num_q):
	n = int(input())
	lst = input()
	lst = [int(x) for x in lst.split()]
	clk = 1
	for i in range(n):
		if lst[i] % n + 1 != lst[(i + 1) % n]:
			clk = 0
			break
	cclk = 1
	for i in range(n):
		if lst[i] != lst[(i + 1) % n] % n + 1:
			cclk = 0
			break
	if clk == 1 or cclk == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	t = arr.index(1)
	if arr[t:] + arr[:t] == list(range(1, n + 1)):
		print('YES')
	elif arr[t + 1:] + arr[:t + 1] == list(range(n, 0, -1)):
		print('YES')
	else:
		print('NO')


N = int(input())
for i in range(N):
	A = int(input())
	flag = True
	B = list(map(int, input().split()))
	for i in range(A):
		if not abs(B[i] - B[(i + 1) % A]) in [1, A - 1]:
			flag = False
	print(['NO', 'YES'][flag])


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	k = l.index(1)
	p = sorted(l)
	if l[k:] + l[:k] == p or l[:k + 1][::-1] + l[k + 1:][::-1] == p:
		print('YES')
	else:
		print('NO')


def main(n, a):
	if n == 1:
		return 'YES'
	index_1 = find(a, 1)
	index = 1
	ans = True
	while index < n:
		if index_1 == n - 1:
			if a[0] != a[index_1] + 1:
				ans = False
				break
			index_1 = 0
			index = index + 1
			continue
		if a[index_1 + 1] != a[index_1] + 1:
			ans = False
			break
		index_1 = index_1 + 1
		index = index + 1
	if ans:
		return 'YES'
	index = 1
	while index < n:
		if index_1 == 0:
			if a[n - 1] != a[index_1] + 1:
				return 'NO'
			index_1 = n - 1
			index = index + 1
			continue
		if a[index_1 - 1] != a[index_1] + 1:
			return 'NO'
		index_1 = index_1 - 1
		index = index + 1
	return 'YES'

def find(arr, i):
	index = 0
	while index < len(arr):
		if arr[index] == i:
			return index
		index = index + 1
N = int(input())
for N_itr in range(N):
	n = int(input())
	a = list(map(int, input().rstrip().split()))
	res = main(n, a)
	print(res)


gi = lambda : list(map(int, input().split()))
for k in range(gi()[0]):
	(n,) = gi()
	l = gi()
	cl = l[l.index(1):] + l[:l.index(1)]
	ccl = l[l.index(n):] + l[:l.index(n)]
	p = [i for i in range(1, n + 1)]
	if cl == p or ccl == p[::-1]:
		print('YES')
	else:
		print('NO')


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations
from itertools import combinations

def li():
	return list(map(int, input().split()))

def num():
	return map(int, input().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	gg = find_gcd(x, y)
	return x * y // gg
mm = 1000000007
yp = 0

def solve():
	t = nu()
	for tt in range(t):
		n = nu()
		a = li()
		x = [0] * n
		for i in range(n):
			x[a[i] - 1] = i + 1
		cc = a[0] - 1
		pp = 1
		fl = True
		while cc < n:
			if x[cc] != pp:
				fl = False
			cc += 1
			pp += 1
		cc = 0
		if a[0] != 1:
			while cc < a[0] - 1:
				if x[cc] != pp:
					fl = False
				cc += 1
				pp += 1
		fl1 = True
		cc = a[0] - 1
		pp = 1
		while cc >= 0:
			if x[cc] != pp:
				fl1 = False
			cc -= 1
			pp += 1
		cc = n - 1
		if a[0] != n:
			while cc > a[0] - 1:
				if x[cc] != pp:
					fl1 = False
				cc -= 1
				pp += 1
		if fl1 == True or fl == True:
			print('YES')
		else:
			print('NO')
solve()


q = int(input())
while q > 0:
	n = int(input())
	x = input().split()
	y = []
	s = ''
	m = ''
	k = ''
	for i in range(0, n):
		y.append(int(x[i]))
		s += x[i] + ','
	s += s
	y = sorted(y, reverse=True)
	z = sorted(y)
	for i in range(0, n):
		m += str(y[i]) + ','
		k += str(z[i]) + ','
	if s.find(k) != -1 or s.find(m) != -1:
		print('YES')
	else:
		print('NO')
	q -= 1


def f(l, n):
	if 1 in l:
		i = l.index(1)
		lx = l[i:] + l[:i]
		l2 = list(range(1, n + 1))
		if lx == l2 or lx == l2[::-1]:
			return 'YES'
		ly = l[i + 1:] + l[:i + 1]
		if ly == l2 or ly == l2[::-1]:
			return 'YES'
	return 'NO'
for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(f(l, n))


q = int(input())

def listRotate(aList, n):
	return aList[n:] + aList[:n]
for i in range(0, q):
	n = input()
	numList = input().split(' ')
	base = numList.index('1')
	numList = listRotate(numList, base)
	left = False
	res = 'YES'
	if len(numList) > 1:
		if numList[1] != '2':
			numList = listRotate(numList, 1)
			left = True
	for j in range(0, len(numList)):
		if left:
			if int(numList[j]) != len(numList) - j:
				res = 'NO'
				break
		elif int(numList[j]) != j + 1:
			res = 'NO'
			break
	print(res)


t = int(input())
while t > 0:
	a = []
	b = []
	toggle = True
	n = int(input())
	a = input().split()
	for item in a:
		b.append(int(item))
	m = sorted(b)
	minx = m[0]
	maxx = m[n - 1]
	for i in range(n - 1):
		if b[i + 1] == b[i] - 1 or b[i + 1] == b[i] + 1 or (b[i] == minx and b[i + 1] == maxx) or (b[i + 1] == minx and b[i] == maxx):
			pass
		else:
			toggle = False
			break
	if toggle == True:
		print('YES')
	else:
		print('NO')
	t -= 1


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	ind = p.index(1)
	p = p[ind:] + p[:ind]
	if all((p[i] + 1 == p[i + 1] for i in range(n - 1))) or all((p[i] == p[i + 1] + 1 for i in range(1, n - 1))):
		print('YES')
	else:
		print('NO')


t = int(input())
for y in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	i1 = x.index(1)
	flag1 = 0
	for j in range(n - 1):
		if x[(i1 + 1) % n] == x[i1] + 1:
			flag1 += 1
		i1 = (i1 + 1) % n
	i2 = x.index(1)
	flag2 = 0
	for j in range(n - 1):
		if x[(i2 - 1) % n] == x[i2] + 1:
			flag2 += 1
		i2 = (i2 - 1) % n
	if flag1 == n - 1 or flag2 == n - 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	if n <= 3:
		print('YES')
	else:
		ans = 1
		for i in range(1, n):
			if abs(a[i] - a[i - 1]) != 1 and abs(a[i] - a[i - 1]) != n - 1:
				ans = 0
				print('NO')
				break
		if ans == 1:
			print('YES')


t = int(input())
while t > 0:
	n = int(input())
	a = [int(x) for x in input().split()]
	p = min(a)
	ind = a.index(p)
	temp = ind
	flag1 = False
	flag2 = False
	for i in range(n - 1):
		if a[temp] + 1 != a[(temp + 1) % n]:
			flag1 = True
			break
		temp = (temp + 1) % n
	temp = ind
	for i in range(n - 1):
		if a[temp] + 1 != a[(temp - 1) % n]:
			flag2 = True
			break
		temp = (temp - 1) % n
	if flag1 and flag2:
		print('NO')
	else:
		print('YES')
	t -= 1


def clockwise(imn, imx):
	if imn > imx:
		if imn - imx > 1:
			return False
		f = p[imn:]
		s = p[:imx + 1]
		if f + s == sorted(f + s):
			return True
	return False

def counterclockwise(imn, imx):
	if imn < imx:
		if imx - imn > 1:
			return False
		f = p[:imn + 1]
		s = p[imx:]
		f = f[::-1]
		s = s[::-1]
		if f + s == sorted(f + s):
			return True
	return False
for _ in range(int(input())):
	n = int(input())
	p = [int(x) for x in input().split()]
	if n <= 3:
		print('YES')
		continue
	if p == sorted(p) or p == sorted(p, reverse=True):
		print('YES')
		continue
	mn = min(p)
	mx = max(p)
	i_mx = p.index(mx)
	i_mn = p.index(mn)
	if clockwise(i_mn, i_mx) or counterclockwise(i_mn, i_mx):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = [int(k) for k in input().split()]
	pos = 0
	for i in range(n):
		if arr[i] == 1:
			pos = i
			break
	clock = 0
	counter = 0
	for i in range(pos, 0, -1):
		if arr[i - 1] - arr[i] != 1:
			clock = 1
			break
	for i in range(n - 1, pos, -1):
		if i == n - 1:
			if arr[n - 1] - arr[0] != 1:
				clock = 1
				break
			else:
				continue
		if arr[i] - arr[i + 1] != 1:
			clock = 1
			break
	for i in range(pos + 1, n):
		if arr[i] - arr[i - 1] != 1:
			counter = 1
			break
	for i in range(pos):
		if arr[i] - arr[i - 1] != 1:
			counter = 1
			break
	if counter and clock:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	p = [int(j) for j in input().split()]
	if n == 1:
		print('YES')
	else:
		(fir, sec) = (p[0], p[1])
		ch = sec > fir
		if sec == n and fir == 1:
			ch = False
		if sec == 1 and fir == n:
			ch = True
		if ch:
			ok = True
			for j in range(1, n):
				if p[j] == p[j - 1] + 1 or (p[j - 1] == n and p[j] == 1):
					pass
				else:
					ok = False
					print('NO')
					break
			if ok:
				print('YES')
		else:
			ok = True
			for j in range(1, n):
				if p[j] == p[j - 1] - 1 or (p[j - 1] == 1 and p[j] == n):
					pass
				else:
					ok = False
					print('NO')
					break
			if ok:
				print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	k = 0
	m = 0
	b = [0] * (n - 1)
	for j in range(n - 1):
		b[j] = a[j] - a[j + 1]
		if b[j] == 1:
			k += 1
		elif b[j] == -1:
			m += 1
		elif b[j] == n - 1:
			m += 1
		elif b[j] == 1 - n:
			k += 1
		else:
			break
	if m == n - 1 or k == n - 1:
		print('YES')
	else:
		print('NO')


def main():
	n = int(input())
	for i in range(0, n):
		k = input()
		val = list((int(v) for v in input().split()))
		is_ok = False
		for j in range(0, len(val)):
			data = val[j:] + val[0:j]
			if sorted(data) == data:
				is_ok = True
			if sorted(data, reverse=True) == data:
				is_ok = True
		if is_ok:
			print('YES')
		else:
			print('NO')
main()


MyDict = {}
for i in range(int(input())):
	N = int(input())
	X = list(map(int, input().split()))
	if N == 1:
		print('YES')
		continue
	Dif = []
	for j in range(1, N):
		Dif.append(X[j] - X[j - 1])
	if (Dif.count(1) == N - 1 or (Dif.count(1) == N - 2 and -(N - 1) in Dif)) or (Dif.count(-1) == N - 1 or (Dif.count(-1) == N - 2 and N - 1 in Dif)):
		print('YES')
		continue
	print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	if n in (1, 2, 3):
		print('YES')
		continue
	i = p.index(1)
	p += p
	if all((p[x] <= p[x + 1] for x in range(i, i + n - 1))) or all((p[x] >= p[x + 1] for x in range(i + 1, i + n))):
		print('YES')
	else:
		print('NO')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		a = LI_()
		ok = n == 1
		t = True
		for i in range(n):
			if a[i] == 0:
				if a[i - 1] != n - 1 or a[(i + 1) % n] != 1:
					t = False
			elif a[i] == n - 1:
				if a[i - 1] != a[i] - 1 or a[(i + 1) % n] != 0:
					t = False
			elif a[i - 1] != a[i] - 1 or a[(i + 1) % n] != a[i] + 1:
				t = False
		ok |= t
		t = True
		for i in range(n):
			if a[i] == 0:
				if a[(i + 1) % n] != n - 1 or a[i - 1] != 1:
					t = False
			elif a[i] == n - 1:
				if a[(i + 1) % n] != a[i] - 1 or a[i - 1] != 0:
					t = False
			elif a[(i + 1) % n] != a[i] - 1 or a[i - 1] != a[i] + 1:
				t = False
		ok |= t
		rr.append(YES(ok))
	return JA(rr, '\n')
print(main())


def main():
	Q = int(input())
	for query in range(Q):
		N = int(input())
		P = [int(i) for i in input().split()]
		if N == 1:
			print('YES')
			continue
		if P[0] < P[1] and (P[0] != 1 or P[1] != N) or (P[0] == N and P[1] == 1):
			pre = P[0]
			for p in P[1:]:
				if pre == N and p == 1:
					pre = p
					continue
				elif pre + 1 == p:
					pre = p
					continue
				else:
					print('NO')
					break
			else:
				print('YES')
		else:
			pre = P[0]
			for p in P[1:]:
				if pre == 1 and p == N:
					pre = p
					continue
				elif pre - 1 == p:
					pre = p
					continue
				else:
					print('NO')
					break
			else:
				print('YES')
main()


import math
q = int(input())
for j in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 4:
		print('YES')
		continue
	normal_array = []
	for i in range(n):
		normal_array.append(i + 1)
	max_ind = 0
	min_ind = 0
	for i in range(n):
		if a[i] == 1:
			min_ind = i
		if a[i] == n:
			max_ind = i
	if math.fabs(max_ind - min_ind) == 1 or math.fabs(max_ind - min_ind) == n - 1:
		if min_ind == 0 or min_ind == n - 1:
			if a == normal_array or a[::-1] == normal_array:
				print('YES')
				continue
			if min_ind == 0:
				for i in range(n - 1, 0, -1):
					if a[i] == n - i + 1:
						pass
					else:
						print('NO')
						break
				else:
					print('YES')
					continue
			if min_ind == n - 1:
				for i in range(0, min_ind):
					if a[i] == i + 2:
						pass
					else:
						print('NO')
						break
				else:
					print('YES')
					continue
		else:
			new_array = []
			if a[min_ind + 1] == 2:
				for i in range(min_ind, n):
					new_array.append(a[i])
				for i in range(0, min_ind):
					new_array.append(a[i])
				if new_array == normal_array:
					print('YES')
					continue
				else:
					print('NO')
					continue
			if a[min_ind - 1] == 2:
				for i in range(min_ind, -1, -1):
					new_array.append(a[i])
				for i in range(n - 1, min_ind, -1):
					new_array.append(a[i])
				if new_array == normal_array:
					print('YES')
					continue
				else:
					print('NO')
					continue
			else:
				print('NO')
				continue
	else:
		print('NO')
		continue


def circularly_identical(list1, list2):
	return ' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2))
q = int(input())
for _ in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	x = list(range(1, n + 1))
	if circularly_identical(a, x) or circularly_identical(a[::-1], x):
		print('YES')
	else:
		print('NO')


n = int(input())
while n > 0:
	n -= 1
	tmp = int(input())
	a = list(map(int, input().split()))
	obr = []
	for i in range(tmp - 1, -1, -1):
		if a[i] > a[i - 1]:
			obr.append(0)
		else:
			obr.append(1)
	if obr.count(0) == 1 or obr.count(1) == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
arr = []
listlen = list()
order = ''
for i in range(n):
	k = int(input())
	listlen.append(k)
	arr1 = list(map(int, input().strip().split(' ')))
	arr.append(arr1)
for i in range(n):
	order = ''
	ordt = ''
	for j in range(listlen[i] - 1):
		if arr[i][j] == 1:
			if arr[i][j + 1] == arr[i][j] + 1:
				order = 'clock'
			elif arr[i][j + 1] == listlen[i]:
				order = 'anticlock'
			else:
				order = 'odd'
		elif arr[i][j] == listlen[i]:
			if arr[i][j + 1] == 1:
				order = 'clock'
			elif arr[i][j + 1] == arr[i][j] - 1:
				order = 'anticlock'
			else:
				order = 'odd'
		elif arr[i][j] + 1 == arr[i][j + 1]:
			order = 'clock'
		elif arr[i][j] - 1 == arr[i][j + 1]:
			order = 'anticlock'
		else:
			order = 'odd'
		if ordt == '':
			ordt = order
		elif ordt == order:
			ordt = order
		else:
			break
	if ordt == order and ordt != 'odd':
		print('Yes')
	else:
		print('No')


import sys

def solve(A):
	n = len(A)
	t = A[0] - 1
	cw = True
	for a in A:
		if t != a - 1:
			cw = False
		t = (t + 1) % n
	t = A[0] - 1
	ccw = True
	for a in A:
		if t != a - 1:
			ccw = False
		t = (n + t - 1) % n
	return ccw or cw
in_file = sys.stdin
q = int(in_file.readline().strip())
for _ in range(q):
	n = int(in_file.readline().strip())
	A = list(map(int, in_file.readline().strip().split()))
	if solve(A):
		sys.stdout.write('YES\n')
	else:
		sys.stdout.write('NO\n')
sys.stdout.flush()


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	i = p.index(1)
	l1 = p[i + 1:] + p[:i]
	l2 = sorted(l1)
	if l1 == l2 or l1 == l2[::-1]:
		print('YES')
	else:
		print('NO')


def check(p):
	n = len(p)
	res = 0
	for i in range(1, n):
		if abs(p[i] - p[i - 1]) == 1:
			res += 1
	if abs(p[n - 1] - p[0]) == 1:
		res += 1
	if res >= n - 1:
		return True
	return False
q = int(input())
for i in range(q):
	n = int(input())
	p = [int(s) for s in input().split(' ')]
	if check(p):
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	m = int(input())
	a = list(map(int, input().split()))
	q = 0
	for i in range(m):
		if abs(a[i] - a[i - 1]) > 1:
			q += 1
	print('YES' if q < 2 else 'NO')


q = int(input())
for t in range(q):
	n = int(input())
	revise = [x for x in range(1, n + 1)]
	clock = revise + revise
	counterclock = list(reversed(revise)) + list(reversed(revise))
	li = list(map(int, input().split(' ')))
	for i in range(0, len(clock) - len(li)):
		flag = False
		if clock[i:i + len(li)] == li[:]:
			flag = True
			break
		elif counterclock[i:i + len(li)] == li[:]:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	n = int(input())
	l = list(map(int, input().split()))
	c = -1
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) == 1 or abs(l[i] - l[i + 1]) == n - 1:
			continue
		else:
			c = 1
			break
	if c == 1:
		print('NO')
	else:
		print('YES')
	t -= 1


import sys
n = int(sys.stdin.readline())
for i in range(n):
	student_num = int(sys.stdin.readline())
	lst = sys.stdin.readline().split()
	for k in range(student_num):
		lst[k] = int(lst[k])
	res = 1
	for j in range(student_num - 1):
		if lst[j] != lst[j + 1] + 1 and lst[j] != lst[j + 1] - 1:
			if lst[j] == 1 and lst[j + 1] == student_num or (lst[j] == student_num and lst[j + 1] == 1):
				res = 1
			else:
				print('NO')
				res = 0
				break
	if res == 1:
		print('YES')


q = int(input())
for k in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	flag1 = True
	flag2 = True
	for i in range(n):
		if a[i] - 1 != a[(i + 1) % n] % n:
			flag1 = False
		if a[i] - 1 != (a[(i + 1) % n] + n - 2) % n:
			flag2 = False
	if flag1 or flag2:
		print('YES')
	else:
		print('NO')


q = int(input())
for c in range(q):
	n = int(input())
	q = [int(x) for x in input().split()]
	res = True
	l = len(q)
	for a in range(l):
		if abs(q[a] - q[(a + 1) % l]) != 1:
			if not (q[a] == 1 and q[(a + 1) % l] == n or (q[a] == n and q[(a + 1) % l] == 1)):
				res = False
	if res:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	cw = True
	ccw = True
	prev = p[0]
	for i in range(1, n):
		if (p[i] - 1) % n != prev % n:
			cw = False
		if p[i] % n != (prev - 1) % n:
			ccw = False
		prev = p[i]
	if cw or ccw:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	d = {}
	l = list(map(int, input().split()))
	for i in range(n):
		try:
			d[l[i] - l[i - 1]] += 1
		except:
			d[l[i] - l[i - 1]] = 1
	if len(d) == 1:
		print('YES')
	elif len(d) == 2:
		m = list(d.values())
		m.sort()
		if m[0] == 1 and m[1] == n - 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for q in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	cl = 0
	ccl = 0
	flag = 0
	if n == 1 or n == 2 or n == 3:
		print('YES')
	else:
		if p[1] == p[0] + 1:
			cl = 1
		elif p[1] == p[0] - 1:
			ccl = 1
		elif p[2] == p[1] + 1:
			cl = 1
		elif p[2] == p[1] - 1:
			ccl = 1
		else:
			flag = 1
		for i in range(1, n):
			if p[i] != p[i - 1] + 1 and cl == 1 and (p[i] != 1):
				flag = 1
			elif p[i] != p[i - 1] - 1 and ccl == 1 and (p[i] != n):
				flag = 1
		if flag == 1:
			print('NO')
		else:
			print('YES')


from sys import stdin
import math
t = int(stdin.readline())
while t:
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	if n == 1:
		print('YES')
	else:
		ans = 'YES'
		flag = 1
		c = 0
		if a[1] > a[0] or a[1] == 1:
			c = 1
			for i in range(1, n):
				if a[i] - a[i - 1] != 1:
					if a[i - 1] - a[i] != n - 1:
						flag = 0
						ans = 'NO'
						break
		if c == 0 or flag == 0:
			flag = 1
			if a[0] > a[1] or a[1] == n:
				for i in range(1, n):
					if a[i - 1] - a[i] != 1:
						if a[i] - a[i - 1] != n - 1:
							flag = 0
							ans = 'NO'
							break
				if flag:
					ans = 'YES'
		print(ans)
	t -= 1


for tc in range(int(input())):
	n = int(input())
	x = [i for i in range(1, n + 1)]
	a = []
	b = []
	l = list(map(int, input().split()))
	d = l.index(1)
	e = d
	for i in range(n):
		a.append(l[d])
		b.append(l[e])
		d = (d + 1) % n
		e = (e - 1) % n
	if (a == x or b == x) or (a == x[::-1] or b == x[::-1]):
		print('YES')
	else:
		print('NO')


def step(idx, to_find=2):
	global a, n
	new_left_idx = idx - 1
	new_right_idx = idx + 1
	if idx == 0:
		new_left_idx = n - 1
	if idx == n - 1:
		new_right_idx = 0
	if a[new_left_idx] == to_find:
		if to_find == n:
			print('YES')
			return
		else:
			step(new_left_idx, to_find + 1)
	elif a[new_right_idx] == to_find:
		if to_find == n:
			print('YES')
			return
		else:
			step(new_right_idx, to_find + 1)
	else:
		print('NO')
		return
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	idx_one = 0
	while idx_one < n:
		if a[idx_one] == 1:
			break
		idx_one += 1
	if n == 1:
		print('YES')
	else:
		step(idx_one)


n = int(input())
for i in range(n):
	k = int(input())
	l = list(map(int, input().split()))
	if k == 1:
		print('YES')
		continue
	flag = 'YES'
	l = l[l.index(1):] + l[:l.index(1)]
	if l[1] > 2:
		l.reverse()
		l = l[-1:] + l[:-1]
	if l != list(range(1, k + 1)):
		flag = 'NO'
	print(flag)


q = int(input())
for j in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or n == 2 or n == 3:
		print('YES')
	else:
		run = 1
		if a[0] % n == (a[n - 1] + 1) % n:
			for i in range(n - 1):
				if not a[i + 1] % n == (a[i] + 1) % n:
					run = 0
					break
		elif a[0] % n == (a[n - 1] - 1) % n:
			for i in range(n - 1):
				if not a[i + 1] % n == (a[i] - 1) % n:
					run = 0
					break
		else:
			run = 0
		if run == 1:
			print('YES')
		else:
			print('NO')


for i in [0] * int(input()):
	a = int(input())
	l = list(map(int, input().split()))
	l = [l[-1]] + l + [l[0]]
	for i in range(1, a + 1):
		if l[i] in [min(l), max(l)]:
			continue
		if abs(l[i] - l[i + 1]) != 1 or abs(l[i] - l[i - 1]) != 1:
			print('NO')
			break
	else:
		print('YES')


from sys import stdin

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())
for _ in range(ii()):
	n = ii()
	a = li()
	p = i = 0
	while i < n:
		if abs(a[i] - a[i - 1]) > 1:
			p += 1
		i += 1
	print('YES' if p < 2 else 'NO')


t = int(input())
for i in range(0, t):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	i = 0
	c = 0
	ac = 0
	for i in range(0, n):
		if arr[i] - arr[(i + 1) % n] == 1:
			ac += 1
		elif arr[(i + 1) % n] - arr[i] == 1:
			c += 1
	if c == n - 1 or ac == n - 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	i = 0
	j = 1
	false = 0
	while j < n:
		if a[j] > n or a[i] > n:
			false = 1
			break
		elif (a[i] == a[j] + 1 or a[i] == a[j] - 1) or (a[i] == n and a[j] == 1) or (a[i] == 1 and a[j] == n):
			i += 1
			j += 1
		else:
			false = 1
			break
	if false == 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for test in range(0, q):
	n = int(input())
	S = input().split()
	if n == 1 or n == 2:
		print('YES')
	else:
		f = 1
		if int(S[1]) - int(S[0]) > 0:
			if int(S[1]) - int(S[0]) == 1:
				r = 'c'
			else:
				r = 'a'
		elif int(S[1]) - int(S[0]) == -1:
			r = 'a'
		else:
			r = 'c'
		if r == 'c':
			for i in range(0, n):
				d = int(S[(i + 1) % n]) - int(S[i])
				if int(S[i]) == n:
					if d != 1 - n:
						f = 0
						break
				elif d != 1:
					f = 0
					break
		else:
			for i in range(0, n):
				d = int(S[(i + 1) % n]) - int(S[i])
				if int(S[i]) == 1:
					if d != n - 1:
						f = 0
						break
				elif d != -1:
					f = 0
					break
		if f == 1:
			print('YES')
		else:
			print('NO')


for x in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr_2 = arr.copy()
	arr_2.sort()

	def pro(arr, arr_2, n):
		count = 0
		for y in range(n - 1):
			if abs(arr[y] - arr[y + 1]) == 1:
				count = count + 1
			elif abs(arr[y] - arr[y + 1]) == abs(arr_2[-1] - arr_2[0]):
				count = count + 1
			else:
				return False
		if count == n - 1:
			return True
		else:
			return False
	aq = pro(arr, arr_2, n)
	if aq:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(o) for o in input().split()]
	arr = [0] * n
	for i in range(n):
		arr[i] = a[i] - a[(i + 1) % n]
	if arr.count(1) == n - 1 or arr.count(-1) == n - 1:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	indexx = a.index(1)
	if a[indexx - 1] == n:
		b = a[indexx:] + a[:indexx]
	else:
		b = a[indexx + 1:] + a[:indexx + 1]
		b.reverse()
	flag = 1
	for i in range(0, n):
		if b[i] != i + 1:
			flag = 0
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ans = -1
	final = -1
	for i in range(1, n - 1):
		if a[i - 1] > a[i] < a[i + 1] or a[i - 1] < a[i] > a[i + 1]:
			ans = i
			break
		elif a[i - 1] < a[i] > a[i + 1]:
			final = i
			break
	if ans == -1:
		print('YES')
	else:
		if ans == -1 and final != -1 or (ans != -1 and final == -1) or (ans != -1 and final != -1):
			x = a[i:] + a[:i]
			y = a[i + 1:] + a[:i + 1]
		if x == sorted(a) or x == sorted(a, reverse=True) or y == sorted(a) or (y == sorted(a, reverse=True)):
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	a = int(input())
	b = list(map(int, input().split()))
	if a == 1 or a == 2:
		print('YES')
	else:
		ans = 0
		c = 0
		c1 = 201
		ans1 = 0
		k = max(b)
		for i in range(a):
			if b[i] > c:
				ans += 1
			c = b[i]
			if b[i] < c1:
				ans1 += 1
			c1 = b[i]
		if b[0] > b[-1]:
			ans += 1
		else:
			ans1 += 1
		if ans >= a or ans1 >= a:
			print('YES')
		else:
			print('NO')


from sys import stdout, stdin
from collections import defaultdict as df
import bisect
import heapq
q = int(input())
for ii in range(q):
	n = int(input())
	p = list(map(int, input().rstrip().split()))
	dp = [0] * (n + 1)
	for i in range(n):
		dp[p[i]] = i + 1
	dp.pop(0)
	y = dp + dp
	ans = []
	r = [i for i in range(1, n + 1)]
	for i in range(len(y)):
		if y[i] == 1:
			ans.append(i)
		if y[i] == n:
			ans.append(i)
	flag = 0
	for i in range(len(y)):
		if i == ans[0]:
			if y[i:i + n] == r or y[i:i + n] == list(reversed(r)):
				flag = 1
				break
		if i == ans[1]:
			if y[i:i + n] == r or y[i:i + n] == list(reversed(r)):
				flag = 1
				break
		if i == ans[2]:
			if y[i:i + n] == r or y[i:i + n] == list(reversed(r)):
				flag = 1
				break
		if i == ans[3]:
			if y[i:i + n] == r or y[i:i + n] == list(reversed(r)):
				flag = 1
				break
	if flag == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	t = int(input())
	lst = [int(x) for x in input().split()]
	mi = min(lst)
	ma = max(lst)
	bigDiff = ma - mi
	got = True
	for j in range(t - 1):
		if abs(lst[j] - lst[j + 1]) == 1 or abs(lst[j] - lst[j + 1]) == bigDiff:
			pass
		else:
			got = False
			break
	if got:
		print('YES')
	else:
		print('NO')


sal = int(input())
for x in range(sal):
	adamIsVeryDumvsdafasdf = input()
	will = list(map(int, input().split()))
	boo = True
	for x in range(1, len(will)):
		if will[x - 1] == will[x] + 1 or will[x - 1] == will[x] - 1 or (will[x - 1] == len(will) and will[x] == 1) or (will[x - 1] == 1 and will[x] == len(will)):
			boo = boo
		else:
			boo = False
	if boo:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	ppl = list(map(int, input().split()))
	ok = 1
	if n == 1:
		print('YES')
		continue
	test = (ppl[1] - ppl[0]) % n
	if test == 1 or test == n - 1:
		for ind in range(1, n - 1):
			if (ppl[ind + 1] - ppl[ind]) % n != test:
				ok = 0
	else:
		ok = 0
	if ok and (ppl[0] - ppl[n - 1]) % n == test:
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(1, n + 1):
	m = int(input())
	query = list(map(int, input().split(' ')))
	isPerm = True
	if m == 1:
		pass
	elif query[1] % m == (query[0] + 1) % m:
		for j in range(2, m):
			if query[j] % m == (query[0] + m + j) % m:
				pass
			else:
				isPerm = False
				break
	elif query[1] % m == (query[0] - 1 + m) % m:
		for j in range(2, m):
			if query[j] % m == (query[0] + m - j) % m:
				pass
			else:
				isPerm = False
				break
	else:
		isPerm = False
	if isPerm:
		print('YES')
	else:
		print('NO')


o = int(input())
for j in range(0, o):
	n = int(input())
	p = input().rstrip().split(' ')
	if len(p) == 1:
		print('YES')
	else:
		V = -1
		for i in range(0, len(p) - 1):
			if abs(int(p[i]) - 1) == int(p[i + 1]):
				continue
			elif abs(int(p[i]) + 1) == int(p[i + 1]):
				continue
			else:
				V = i + 1
				break
		if V == -1:
			print('YES')
		else:
			T = p[V:len(p)]
			B = p[0:V]
			C = T + B
			N = list(C)
			N.sort(key=int)
			F = 0
			for i in range(0, len(N)):
				if N[i] == C[i]:
					continue
				else:
					F = 1
					break
			if F == 0:
				print('YES')
			else:
				N.reverse()
				for i in range(0, len(N)):
					if N[i] == C[i]:
						continue
					else:
						F = 2
						break
				if F != 2:
					print('YES')
				else:
					print('NO')


q = int(input(''))
for v in range(q):
	n = int(input(''))
	a = list(map(int, input('').split(' ')))
	res = True
	res1 = True
	d = []
	for i in range(n - 1):
		d.append(a[i + 1] - a[i])
	for i in range(n - 1):
		if d[i] == -1:
			res = True
		elif d[i] == n - 1:
			res = True
		else:
			res = False
			break
	for i in range(n - 1):
		if d[i] == 1:
			res1 = True
		elif d[i] == 1 - n:
			res1 = True
		else:
			res1 = False
			break
	if res | res1:
		print('YES')
	else:
		print('NO')


def clockwise(student, student_num):
	counter = 0
	for i in range(0, student_num + 1):
		if student[(i + 1) % student_num] == student[i % student_num] + 1:
			counter += 1
	if counter >= student_num - 1:
		return True

def counterclockwise(student, student_num):
	counter = 0
	for i in range(0, student_num + 1):
		if student[(i + 1) % student_num] == student[i % student_num] - 1:
			counter += 1
	if counter >= student_num - 1:
		return True

def test(student, student_num):
	if clockwise(student, student_num) == True or counterclockwise(student, student_num) == True:
		print('YES')
	else:
		print('NO')
query = int(input())
for h in range(0, query):
	student_num = int(input())
	student = input().split()
	for i in range(0, student_num):
		student[i] = int(student[i])
	test(student, student_num)


q = int(input())

def cw(p, n):
	if n == 1 or n == 2:
		return True
	j = p.index(1)
	p = p[j:] + p[:j]
	for i in range(1, n):
		if p[i] != p[i - 1] + 1:
			return False
	return True

def ccw(p, n):
	return cw(p[::-1], n)
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	if cw(p, n) or ccw(p, n):
		print('YES')
	else:
		print('NO')


def mymin(list):
	m = list[0]
	index = 0
	for i in range(1, len(list)):
		if m > list[i]:
			m = list[i]
			index = i
	return index

def main(list):
	index = mymin(list)
	left = list[:index]
	list = list[index:] + left
	answer = 1
	now = list[0]
	for i in range(1, len(list)):
		next = list[i]
		if next == now + 1:
			now = next
		else:
			answer = 0
			break
	if answer:
		return True
	now = list[0]
	answer = 1
	for i in range(len(list) - 1, 0, -1):
		next = list[i]
		if next == now + 1:
			now = next
		else:
			answer = 0
			break
	return answer
for i in range(int(input())):
	q = int(input())
	if main(list(map(int, input().split()))):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		if a[i] == 1:
			ind = i
	i = 0
	ans = False
	f = True
	while i != n - 1:
		if a[ind] + 1 == a[(ind + 1) % n]:
			ind = (ind + 1) % n
		else:
			f = False
		i += 1
	i = 0
	ans = ans or f
	f = True
	while i != n - 1:
		if a[(ind - 1 + n) % n] == a[ind] + 1:
			ind = (ind - 1 + n) % n
		else:
			f = False
		i += 1
	ans = ans or f
	if ans:
		print('YES')
	else:
		print('NO')


import math
import sys
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)
q = ii()
for i in range(q):
	n = ii()
	d = li()
	d.append(d[0])
	f = r = True
	for i in range(n):
		if abs(d[i + 1] - d[i]) != 1:
			if f and abs(d[i + 1] - d[i]) == n - 1:
				f = False
			else:
				r = False
				break
	print('YES') if r else print('NO')


import copy
from copy import deepcopy
q = int(input())
for k in range(q):
	N = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	c = deepcopy(b)
	c.reverse()
	f = False
	tmp = deepcopy(a)
	for i in range(N):
		tmp = tmp[1:] + [tmp[0]]
		if tmp == b:
			f = True
	for i in range(N):
		a = [a[-1]] + a[:-1]
		if a == c:
			f = True
	if f:
		print('Yes')
	else:
		print('No')


for q in range(int(input())):
	(n, p) = (int(input()), list(map(int, input().split())))
	d = {p[i + 1] - p[i] for i in range(n - 1)}
	print('YES' if d in (set(), {1}, {1, 1 - n}, {-1}, {-1, n - 1}) else 'NO')


ll = lambda : list(map(int, input().split()))
testcases = 1
testcases = int(input())
for testcase in range(testcases):
	[n] = ll()
	a = ll()
	ok = 1
	for i in range(n):
		for j in range(i, i + n - 1):
			if a[j % n] - a[(j + 1) % n] == 1:
				continue
			else:
				break
		else:
			print('YES')
			break
	else:
		for i in range(n):
			for j in range(i, i + n - 1):
				if a[j % n] - a[(j + 1) % n] == -1:
					continue
				else:
					break
			else:
				print('YES')
				break
		else:
			print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	for i in range(n):
		if a[i] == 1:
			x = i
			break
	ans = True
	last = a[x]
	for i in range(x + 1, n):
		last = a[i]
		if not a[i] == a[i - 1] + 1:
			ans = False
			break
	if ans:
		for i in range(x):
			if not a[i] == last + 1:
				ans = False
				break
			last = a[i]
	if ans:
		print('YES')
	else:
		a = a[::-1]
		x = 0
		for i in range(n):
			if a[i] == 1:
				x = i
				break
		ans = True
		last = a[x]
		for i in range(x + 1, n):
			last = a[i]
			if not a[i] == a[i - 1] + 1:
				ans = False
				break
		if ans:
			for i in range(x):
				if not a[i] == last + 1:
					ans = False
					break
				last = a[i]
		if ans:
			print('YES')
		else:
			print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	A = list(map(int, input().split()))
	for i in range(n - 1):
		if abs(A[i] - A[i + 1]) == 1:
			continue
		elif (A[i + 1] == 1 or A[i] == 1) and (A[i + 1] == n or A[i] == n):
			continue
		else:
			print('NO')
			break
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = 1
	c2 = 1
	for i in range(n - 1):
		if a[i + 1] - a[i] != 1 and a[i] != n:
			c1 = 0
		if a[i] - a[i + 1] != 1 and a[i + 1] != n:
			c2 = 0
	if a[0] - a[n - 1] != 1 and a[n - 1] != n:
		c1 = 0
	if a[0] + 1 != a[n - 1] and a[0] != n:
		c2 = 0
	if c1 == 1 or c2 == 1:
		print('YES')
	else:
		print('NO')


def ans(n, arr):
	one = 0
	count = 2
	reverse = 0
	if n == 1:
		return 'YES'
	for i in range(n):
		if arr[i] == 1:
			one = i
	if arr[(one + 1) % n] == 2:
		reverse = 1
	elif arr[(one - 1) % n] == 2:
		reverse = -1
	else:
		return 'NO'
	if reverse == 1:
		while count != n:
			one += 1
			if arr[one % n] == count:
				count += 1
			else:
				return 'NO'
	else:
		while count != n:
			one -= 1
			if arr[one % n] == count:
				count += 1
			else:
				return 'NO'
	return 'YES'
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(ans(n, arr))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	while a[0] != 1:
		a.append(a.pop(0))
	if len(a) != 1:
		if a[1] == n:
			a.append(a.pop(0))
			a.reverse()
		if a == sorted(a):
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


q = int(input())
for w in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	(ok1, ok2) = (1, 1)
	for i in range(1, n):
		if a[i] != a[i - 1] + 1 and (not abs(a[i] - a[i - 1]) == n - 1):
			ok1 = 0
		if a[i] != a[i - 1] - 1 and (not abs(a[i] - a[i - 1]) == n - 1):
			ok2 = 0
	if ok1 + ok2 > 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	flag = 0
	l = [int(x) for x in input().split()]
	if len(l) == 1:
		print('YES')
	else:
		for i in range(1, len(l)):
			if abs(l[i] - l[i - 1]) == 1 or abs(l[i] - l[i - 1]) == n - 1:
				continue
			else:
				flag = 1
				break
		if flag == 1:
			print('NO')
		else:
			print('YES')


import math, sys
t = int(input())
while t != 0:
	t -= 1
	n = int(input())
	arr = list(map(int, input().split()))
	one = arr.index(1)
	d = -1 if arr[one - 1] - 1 == arr[one] else 1
	one += d
	ans = [1]
	while len(ans) != n:
		if one < 0:
			one = n - 1
			d = -1
		if one > n:
			one = 0
			d = 1
		if one >= 0 and one <= n - 1:
			ans.append(arr[one])
		one += d
	c = 1
	p = 'YES'
	for i in range(n):
		if ans[i] != c:
			p = 'NO'
		c += 1
	print(p)


for _ in range(int(input())):
	n = int(input())
	lst = []
	for i in range(1, n + 1):
		lst.append(i)
	a = list(map(int, input().split()))
	s = 0
	for i in range(len(a) + 1):
		if a[i:] + a[:i] == lst or list(reversed(a[i:] + a[:i])) == lst:
			s += 1
	if s == 0:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = l[:]
	l2 = sorted(l)
	l3 = sorted(l, reverse=True)
	while n:
		if l2 == l1 or l3 == l1:
			print('YES')
			break
		l1.append(l1.pop(0))
		n -= 1
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [arr[i] - arr[i + 1] for i in range(n - 1)]
	if arr.count(1) >= n - 2 or arr.count(-1) >= n - 2:
		print('YES')
	else:
		print('NO')


cs = int(input())
for _ in range(cs):
	n = int(input())
	l = list(map(int, input().split()))
	ind = l.index(1)
	f = 1
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) == 1:
			f += 1
	if abs(l[-1] - l[0]) == 1:
		f += 1
	if f >= n:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	arr = 1
	p = [int(x) for x in input().split()]
	for i in range(1, n):
		if p[i] != p[i - 1] + 1 and p[i] != 1:
			break
	else:
		arr *= 0
	for i in range(1, n):
		if p[i] != p[i - 1] - 1 and p[i] != n:
			break
	else:
		arr *= 0
	if arr == 0:
		print('YES')
	else:
		print('NO')


q = int(input())
for query in range(q):
	n = int(input())
	a = list(map(int, input().split(' ')))
	flag = 0
	for i in range(n - 1):
		if a[i + 1] - a[i] != 1:
			if a[i + 1] - a[i] == 1 - n:
				continue
			else:
				flag = 1
				break
	if flag == 0:
		print('YES')
		continue
	for i in range(n - 1):
		if a[i] - a[i + 1] != 1:
			if a[i] - a[i + 1] == 1 - n:
				continue
			else:
				flag = 2
				break
	if flag == 1:
		print('YES')
		continue
	print('NO')


from sys import stdin, stdout
from heapq import heapify, heappush, heappop, heappushpop
from collections import defaultdict as dd, deque as dq, Counter as C
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations as cmb, permutations as pmb
from math import factorial as f, ceil, gcd, sqrt, log, inf
mi = lambda : map(int, input().split())
ii = lambda : int(input())
li = lambda : list(map(int, input().split()))
mati = lambda r: [li() for _ in range(r)]
lcm = lambda a, b: a * b // gcd(a, b)
MOD = 10 ** 9 + 7

def soe(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def solve():
	n = ii()
	arr = li()
	p = [x for x in range(1, n + 1)]
	m = arr.index(1)
	ans = False
	(a, b) = (0, 0)
	cnt = 1
	l = 0
	while a <= n:
		if arr[m] == cnt:
			l += 1
		cnt += 1
		a += 1
		m -= 1
		if m == -1:
			m = n - 1
	m = arr.index(1)
	cnt = 1
	k = 0
	while b <= n:
		if arr[m] == cnt:
			k += 1
		cnt += 1
		b += 1
		m += 1
		if m == n:
			m = 0
	if l == n or k == n:
		print('YES')
	else:
		print('NO')
for _ in range(ii()):
	solve()


from sys import stdin, stdout
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	p = list(map(int, stdin.readline().split()))
	idx = p.index(1)
	q = list(range(1, n + 1))
	r = list(range(n, 0, -1))
	x = p[idx + 1:] + p[:idx + 1]
	y = p[idx:] + p[:idx]
	if x == q:
		stdout.write('YES\n')
		continue
	if x == r:
		stdout.write('YES\n')
		continue
	if y == q:
		stdout.write('YES\n')
		continue
	if y == r:
		stdout.write('YES\n')
		continue
	stdout.write('NO\n')


import copy
t = int(input())

def func(na):
	ana = copy.deepcopy(na)
	ana.sort()
	return na == ana or ana == na[::-1]
while t:
	n = int(input())
	a = list(map(int, input().split()))
	pos = -1
	for i in range(0, n):
		if a[i] == 1:
			pos = i
			break
		else:
			continue
	na = a[pos + 1:n] + a[0:pos]
	ana = copy.deepcopy(na)
	ana.sort()
	if func(na):
		print('YES')
	else:
		print('NO')
	t -= 1


q = int(input())
while q > 0:
	n = int(input())
	a = [int(v) for v in input().split()]
	posone = -1
	for i in range(n):
		if a[i] == 1:
			posone = i
			break
	rightone = (i + 1) % n
	leftone = n - 1 if i - 1 < 0 else i - 1
	pl = i
	pr = i
	leftf = 0
	rightf = 0
	while a[rightone] != 1:
		if a[rightone] - a[pr] != 1:
			rightf = 1
			break
		pr = rightone
		rightone = (rightone + 1) % n
	if rightf == 0:
		print('YES')
		q -= 1
		continue
	while a[leftone] != 1:
		if a[leftone] - a[pl] != 1:
			leftf = 1
			break
		pl = leftone
		leftone = n - 1 if leftone - 1 < 0 else leftone - 1
	if leftf == 0:
		print('YES')
	else:
		print('NO')
	q -= 1


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	d = l.index(1)
	x = sorted(l)
	if l[d:] + l[:d] == x or l[:d + 1][::-1] + l[d + 1:][::-1] == x:
		print('YES')
	else:
		print('NO')


def func(list1, list2):
	return ' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2))
t = int(input())
for _ in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	l = p[:]
	ll = p[:]
	l.sort()
	ll.sort(reverse=True)
	if func(l, p):
		print('YES')
	elif func(ll, p):
		print('YES')
	else:
		print('NO')


queries = int(input())
for i in range(0, queries):
	number = int(input())
	permutation = input()
	permutation_1 = permutation.split(' ')
	permutation_array = []
	for i in permutation_1:
		permutation_array.append(int(i))
	new_array_c = []
	new_array_ac = []
	for i in range(0, number):
		if permutation_array[i] == number:
			new_array_c.extend(permutation_array[i + 1:number])
			new_array_c.extend(permutation_array[0:i + 1])
			new_array_ac.extend(permutation_array[i:number])
			new_array_ac.extend(permutation_array[0:i])
			break
	sample_array_1 = []
	sample_array_2 = []
	for i in range(1, number + 1):
		sample_array_1.append(i)
		sample_array_2.append(number + 1 - i)
	if new_array_c == sample_array_1 or new_array_ac == sample_array_2:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	t = 1
	l = list(map(int, input().split()))
	x = l.index(1)
	a = all((l[(x + j) % n] < l[(x + j + 1) % n] for j in range(1, n - 1)))
	b = all((l[(x + j) % n] > l[(x + j + 1) % n] for j in range(1, n - 1)))
	if a or b:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	A = input()
	A += ' ' + A
	v1 = ' '.join(map(str, range(1, n + 1)))
	v2 = ' '.join(map(str, range(n, 0, -1)))
	if v1 in A or v2 in A:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	if n == 1:
		print('YES')
		continue
	for i in range(n - 1):
		if (a[i] + 1) % n != a[i + 1]:
			break
	else:
		print('YES')
		continue
	for i in range(n - 1):
		if (a[i] - 1) % n != a[i + 1]:
			break
	else:
		print('YES')
		continue
	print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(i) for i in input().split()]
	p = p[p.index(1):] + p[:p.index(1)]
	poss = True
	for i in range(1, len(p)):
		if p[i] - p[i - 1] != 1:
			poss = False
	if not poss:
		poss = True
		for i in range(2, len(p)):
			if p[i] - p[i - 1] != -1:
				poss = False
	print('YES' if poss else 'NO')


for qw in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split(' ')))
	if n == 1:
		print('YES')
		continue
	else:
		inc = -1
		if arr[1] == arr[0] + 1 or (arr[0] == n and arr[1] == 1):
			inc = 1
		flag = True
		nexti = arr[0] + inc
		for i in range(0, n - 1):
			if nexti == 0:
				nexti = n
			elif nexti == n + 1:
				nexti = 1
			if arr[i + 1] != nexti:
				flag = False
				break
			else:
				nexti += inc
	if flag:
		print('YES')
	else:
		print('NO')


q = int(input())
arr = []
for i in range(q):
	arr.append([])
	arr[i].append(int(input()))
	arr[i].append([int(s) for s in input().split()])
for i in range(q):
	flag1 = True
	flag2 = True
	n = arr[i][0]
	myArr = arr[i][1]
	index1 = myArr.index(1)
	for _ in range(n - 1):
		if index1 != n - 1:
			if myArr[index1] + 1 == myArr[index1 + 1]:
				index1 += 1
			else:
				flag1 = False
				break
		else:
			if myArr[index1] + 1 == myArr[0]:
				index1 += 1
			else:
				flag1 = False
				break
			index1 = 0
	index1 = myArr.index(1)
	for _ in range(n - 1):
		if index1 != 0:
			if myArr[index1] + 1 == myArr[index1 - 1]:
				index1 -= 1
			else:
				flag2 = False
				break
		else:
			if myArr[index1] + 1 == myArr[n - 1]:
				index1 -= 1
			else:
				flag2 = False
				break
			index1 = n - 1
	if flag1 or flag2:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ind = a.index(1)
	flag = False
	req_num = 1
	i = ind
	count = 0
	while True:
		if a[i % n] == req_num:
			req_num += 1
		else:
			flag = True
			break
		i += 1
		count += 1
		if count == n:
			break
	if flag:
		ind = a.index(1)
		flag = False
		req_num = 1
		i = ind
		count = 0
		while True:
			if a[i % n] == req_num:
				req_num += 1
			else:
				flag = True
				break
			i -= 1
			count += 1
			if count == n:
				break
	if flag:
		print('NO')
	else:
		print('YES')


n = int(input())
for x in range(n):
	q = int(input())
	ind = [int(x) for x in input().split()]
	canDo = True
	for y in range(q):
		v1 = abs(ind[y - 1] - ind[y])
		v2 = abs(ind[(y + 1) % q] - ind[y])
		if v1 > 1 and v1 < q - 1 or (v2 > 1 and v2 < q - 1):
			canDo = False
			break
	print('YES' if canDo else 'NO')


q = int(input())
while q:
	q = q - 1
	n = int(input())
	l = list(map(int, input().split()))
	ch1 = 0
	ch2 = 0
	for i in range(n - 1):
		if l[i + 1] - l[i] == 1:
			continue
		elif l[i] == n and l[i] - l[i + 1] == n - 1:
			continue
		else:
			ch1 = 1
			break
	for i in range(n - 1):
		if l[i] - l[i + 1] == 1:
			continue
		elif l[i] == 1 and l[i + 1] - l[i] == n - 1:
			continue
		else:
			ch2 = 1
			break
	if ch1 == 1 and ch2 == 1:
		print('NO')
	else:
		print('YES')


for u in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	(x, y) = (0, 0)
	for i in range(n - 1):
		if l[i] != l[i + 1] - 1:
			x = x + 1
		if l[i] != l[i + 1] + 1:
			y = y + 1
	if x > 1 and y > 1:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: int(x) - 1, input().split()))
	a += [a[0]]
	if all(((a[i] + 1) % n == a[i + 1] % n for i in range(n))) or all(((a[i] - 1) % n == a[i + 1] % n for i in range(n))):
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(0, T):
	N = int(input())
	s = [int(x) for x in input().split()]
	ptr = -1
	for j in range(0, len(s)):
		if s[j] == 1:
			ptr = j
			break
	L1 = s[ptr:] + s[:ptr]
	L2 = s[:ptr + 1][::-1] + s[ptr + 1:][::-1]
	arr = []
	for j in range(1, len(s) + 1):
		arr.append(j)
	if L1 != arr and L2 != arr:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	n = int(input())
	arr = [int(s) for s in input().split()]
	k = arr.index(1)
	(a1, a2) = (1, 1)
	for i in range(1, n + 1):
		if arr[(k + i - 1) % n] != i:
			a1 = 0
		if arr[(k - i + 1) % n] != i:
			a2 = 0
	print('YES' if a1 + a2 > 0 else 'NO')


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
		t = t - 1
		continue
	f = 0
	c = 0
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) != 1:
			f += 1
			if f == 2:
				print('NO')
				c = 1
				break
	if c == 0:
		if f == 0:
			print('YES')
		elif abs(l[-1] - l[0]) == 1:
			print('YES')
		else:
			print('NO')
	t = t - 1


test = int(input())
while test > 0:
	n = int(input())
	num = list(map(int, input().split()))
	fal = False
	for i in range(0, n):
		coun = 1
		j = i
		if num[i] == 1:
			while num[j % n] == coun:
				j += 1
				coun += 1
			j = i
			if coun > n:
				fal = True
				break
			coun = 1
			while num[(j + n) % n] == coun:
				j -= 1
				coun += 1
			if coun > n:
				fal = True
				break
	if fal == True:
		print('YES')
	else:
		print('NO')
	test -= 1


t = int(input())
while t > 0:
	n = int(input())
	a = list(map(int, input().split()))
	acen = []
	desc = []
	for i in range(1, n + 1):
		acen.append(i)
		desc.append(n + 1 - i)
	for i in range(1, n + 1):
		acen.append(i)
		desc.append(n + 1 - i)
	i = 0
	j = 0
	f = 1
	k = 0
	while j < 2 * n and i < 2 * n:
		if a[0] != acen[i]:
			i = i + 1
		else:
			j = i
			while k < n:
				if a[k] == acen[j]:
					k = k + 1
					j = j + 1
				else:
					f = 0
					break
			break
	if f == 1:
		print('YES')
	else:
		i = 0
		j = 0
		k = 0
		f = 1
		while j < 2 * n and i < 2 * n:
			if a[0] != desc[i]:
				i = i + 1
			else:
				j = i
				while k < n:
					if a[k] == desc[j]:
						k = k + 1
						j = j + 1
					else:
						f = 0
						break
				break
		if f == 1:
			print('YES')
		else:
			print('NO')
	t = t - 1


temp = 1
token = 0
student = []
total_Queries = int(input())
while temp <= total_Queries:
	total_students = int(input())
	stud = input().split(' ')
	for i in range(total_students):
		student.insert(i, int(stud[i]))
	for i in range(total_students):
		try:
			if student[i] + 1 == student[i + 1]:
				token = token + 1
		except:
			if student[0] + 1 == student[-1]:
				token = token + 1
			elif student[i] + 1 == student[-1]:
				token = token + 1
	for i in range(total_students):
		try:
			if student[i] - 1 == student[i + 1]:
				token = token + 1
		except:
			if student[0] - 1 == student[-1]:
				token = token + 1
			elif student[i] - 1 == student[-1]:
				token = token + 1
	if token < total_students - 1:
		print('NO')
		token = 0
	else:
		print('YES')
		token = 0
	student.clear()
	temp = temp + 1


t = int(input())
while t > 0:
	n = int(input())
	l = list(map(int, input().split(' ')))
	i = 0
	p = 0
	while i < n:
		if abs(l[i] - l[i - 1]) > 1:
			p = p + 1
		i = i + 1
	if p < 2:
		print('YES')
	else:
		print('NO')
	t = t - 1


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	cnt = 0
	for i in range(n):
		if a[i] >= a[(i + 1) % n]:
			cnt += 1
	cnt2 = 0
	for i in range(n):
		if a[i] <= a[(i + 1) % n]:
			cnt2 += 1
	if cnt <= 1:
		print('YES')
	elif cnt2 <= 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	ans = 0
	a = list(map(int, input().split()))
	for i in range(1, n):
		if a[i - 1] != n and a[i] == a[i - 1] + 1:
			continue
		elif a[i - 1] == n and a[i] == 1:
			continue
		else:
			ans = -1
			break
	if ans == -1:
		for i in range(1, n):
			if a[i - 1] != 1 and a[i - 1] == a[i] + 1:
				if i == n - 1:
					ans = 0
				continue
			elif a[i - 1] == 1 and a[i] == n:
				if i == n - 1:
					ans = 0
				continue
			else:
				ans = -1
				break
	if ans == -1:
		print('NO')
	else:
		print('YES')


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	ans = True
	for i in range(n - 1):
		if abs(l[i + 1] - l[i]) in [1, n - 1]:
			continue
		else:
			ans = False
	if ans:
		print('YES')
	else:
		print('NO')


test = int(input())
for i in range(test):
	n = int(input())
	l = list(map(int, input().split()))
	while 1:
		if l[n - 1] != 1:
			l = [l[n - 1]] + l[:n - 1]
		else:
			break
	if l[n - 2] != 2:
		l = [1] + l[:n]
		c = 0
		for i in range(n - 1):
			if l[i + 1] - l[i] != 1:
				c = 1
		if c == 0:
			print('YES')
		else:
			print('NO')
	else:
		c = 0
		for i in range(n - 1):
			if l[i] - l[i + 1] != 1:
				c = 1
		if c == 0:
			print('YES')
		else:
			print('NO')


test_cases = int(input())
for x in range(test_cases):
	a = True
	b = input()
	circle = [int(i) for i in input().split()]
	if sorted(circle) == circle or sorted(circle, reverse=True) == circle:
		print('YES')
		a = False
	else:
		if int(circle[-1]) + 1 == int(circle[0]) and a == True:
			a = False
			while int(circle[-1]) + 1 == int(circle[0]):
				x = circle[-1]
				circle.pop(-1)
				circle.insert(0, x)
			if circle == sorted(circle):
				print('YES')
			else:
				print('NO')
		if int(circle[-1]) - 1 == int(circle[0]) and a == True:
			a = False
			while int(circle[-1]) - 1 == int(circle[0]):
				x = circle[-1]
				circle.pop(-1)
				circle.insert(0, x)
			if circle == sorted(circle, reverse=True):
				print('YES')
			else:
				print('NO')
		if circle != sorted(circle) and circle != sorted(circle, reverse=True) and (a == True):
			print('NO')


w = int(input())

def fun(x, n):
	if x % n != 0:
		return x % n
	else:
		return n
for we in range(w):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		start = l[0]
		odp = 1
		if l[1] == fun(l[0] + 1, n):
			for i in range(1, n):
				if l[i] != fun(l[i - 1] + 1, n):
					odp = 0
					break
		elif l[1] == fun(l[0] - 1, n):
			for i in range(1, n):
				if l[i] != fun(l[i - 1] - 1, n):
					odp = 0
					break
		else:
			odp = 0
		if odp == 0:
			print('NO')
		else:
			print('YES')


a = int(input())
for x in range(a):
	b = int(input())
	c = list(map(int, input().split()))
	d = c.copy()
	d.sort()
	j = []
	h = []
	p = c.index(1)
	m = p
	r = p
	while True:
		if len(j) == b:
			break
		if m == b - 1:
			j.append(c[m])
			m = 0
		else:
			j.append(c[m])
			m += 1
	while True:
		if len(h) == b:
			break
		if r == 0:
			h.append(c[r])
			r = b - 1
		else:
			h.append(c[r])
			r -= 1
	if j == d or h == d:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	N = int(input())
	A = [int(i) for i in input().split()]
	pos = A.index(1)
	cond1 = True
	for i in range(N - 1):
		if A[(pos + i) % N] > A[(pos + i + 1) % N]:
			cond1 = False
	cond2 = True
	for i in range(N - 1):
		if A[(pos - i) % N] > A[(pos - i - 1) % N]:
			cond2 = False
	if cond1 or cond2:
		print('YES')
	else:
		print('NO')


from sys import stdin, stdout
q = int(stdin.readline())
for i in range(q):
	n = int(stdin.readline())
	arr = [int(x) for x in stdin.readline().split()]
	if n == 1:
		stdout.write('YES' + '\n')
		continue
	valid_enc = 0
	for j in range(n - 1):
		diff = arr[j + 1] - arr[j]
		if abs(diff) == 1 or abs(diff) == n - 1:
			continue
		else:
			valid_enc = 1
	if valid_enc == 0:
		stdout.write('YES' + '\n')
	else:
		stdout.write('NO' + '\n')


n = int(input())
for _ in range(n):
	_ = int(input())
	(*l,) = map(int, input().split())
	ans = False
	for i in range(len(l) - 1):
		if l[i] == len(l):
			if not l[i + 1] == 1:
				break
		elif not l[i] + 1 == l[i + 1]:
			break
	else:
		ans = True
	for i in range(len(l) - 1):
		if l[i] == 1:
			if not l[i + 1] == len(l):
				break
		elif not l[i] - 1 == l[i + 1]:
			break
	else:
		ans = True
	if ans:
		print('YES')
	else:
		print('NO')


def clockwise(a):
	n = len(a)
	count = 0
	for i in range(n - 1):
		if a[i] > a[i + 1]:
			count += 1
	if count == 1 and a[-1] < a[0]:
		return True
	if count == 0:
		return True
	return False

def anticlockwise(a):
	n = len(a)
	count = 0
	for i in range(n - 1):
		if a[i] < a[i + 1]:
			count += 1
	if count == 1 and a[-1] > a[0]:
		return True
	if count == 0:
		return True
	return False
q = int(input())
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	if clockwise(p) or anticlockwise(p):
		print('YES')
	else:
		print('NO')


t = int(input())
for x in range(t):
	n = int(input())
	l1 = [int(x) for x in input().split()]
	i = l1.index(1)
	j = 0
	ruinedf = 0
	while j < n - 1:
		if l1[i % n] + 1 != l1[(i + 1) % n]:
			ruinedf = 1
			break
		i += 1
		j += 1
	if not ruinedf:
		print('YES')
	else:
		l1.reverse()
		i = l1.index(1)
		ruinedf = 0
		j = 0
		while j < n - 1:
			if l1[i % n] + 1 != l1[(i + 1) % n]:
				ruinedf = 1
				break
			i += 1
			j += 1
		if not ruinedf:
			print('YES')
		else:
			print('NO')


import math

def is_possible(arr):
	if len(arr) == 1:
		return 'YES'
	if (arr[1] - arr[0]) % len(arr) == 1:

		def op(x, y):
			return y - x
	else:

		def op(x, y):
			return x - y
	for i in range(len(arr) - 1):
		if op(arr[i], arr[i + 1]) % len(arr) != 1:
			return 'NO'
	return 'YES'
q = int(input())
for i in range(q):
	input()
	arr = list(map(int, input().split()))
	print(is_possible(arr))


def solve(n, arr):
	if n <= 3:
		return 'YES'
	else:
		diff1 = arr[0] - arr[1]
		if abs(diff1) != 1:
			diff1 = arr[1] - arr[2]
			if abs(diff1) != 1:
				return 'NO'
		if diff1 < 0:
			diff2 = n - 1
		else:
			diff2 = 1 - n
	for i in range(1, n):
		newdiff = arr[i - 1] - arr[i]
		if newdiff == diff1 or newdiff == diff2:
			pass
		else:
			return 'NO'
	return 'YES'

def main():
	n = int(input())
	for _ in range(n):
		num = int(input())
		arr = list(map(int, input().rstrip().split()))
		ans = solve(num, arr)
		print(ans)
main()


import sys
import math
input = sys.stdin.readline

def check(arr, n):
	if n == 1 or n == 2:
		return True
	flag = 0
	for i in range(1, n):
		if abs(arr[i] - arr[i - 1]) == n - 1 and flag == 0:
			flag = 1
		elif abs(arr[i] - arr[i - 1]) == 1:
			flag = flag
		elif abs(arr[i] - arr[i - 1]) > 1 and flag == 1:
			return False
		else:
			return False
	return True
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if check(arr, n):
		print('YES')
	else:
		print('NO')


q = int(input())
for m in range(q):
	n = int(input())
	line = input().split()
	p = [int(x) for x in line]
	clock = True
	cclock = True
	for i in range(n - 1):
		cur = p[i]
		nxt = p[i + 1]
		if cur != n and cur + 1 != nxt:
			clock = False
		if cur != 1 and cur - 1 != nxt:
			cclock = False
		if not clock and (not cclock):
			break
	if clock or cclock:
		print('YES')
	else:
		print('NO')


import sys
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
from heapq import heappush as hpush
from heapq import heappop as hpop
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
for _ in range(ri(1)):
	n = ri(1)
	a = ri()
	idx = a.index(1)
	check1 = 1
	check2 = 1
	for i in range(n):
		if a[(idx + i) % n] == i + 1:
			pass
		else:
			check1 = 0
	a = a[::-1]
	idx = a.index(1)
	for i in range(n):
		if a[(idx + i) % n] == i + 1:
			pass
		else:
			check2 = 0
	if check1 == 1 or check2 == 1:
		print('YES')
	else:
		print('NO')


x = int(input())
input1 = []
input2 = []
for i in range(x):
	k = int(input())
	input1.append(k)
	seq = list(map(int, input().split()))
	input2.append(seq)
length = len(input1)
for i in range(length):
	count1 = 0
	count2 = 0
	p = input1[i]
	q = input2[i]
	if p == 1:
		print('YES')
	else:
		inc = 0
		dec = 0
		q.append(q[0])
		for j in range(p):
			if q[j] > q[j + 1]:
				inc += 1
			else:
				dec += 1
		if min(inc, dec) > 1:
			print('NO')
		else:
			print('YES')


q = int(input())
result = []
for _ in range(q):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		result.append('YES')
		continue
	out = 'YES'
	i = 0
	if arr[0] + 1 == arr[1] or (arr[0] == n and arr[1] == 1):
		while i < n:
			if arr[i - 1] + 1 != arr[i] and arr[i - 1] != n:
				out = 'NO'
				break
			i += 1
	else:
		while i < n:
			if arr[i - 1] - 1 != arr[i] and arr[i - 1] != 1:
				out = 'NO'
				break
			i += 1
	result.append(out)
for i in range(q):
	print(result[i])


n = int(input())
for i in range(n):
	num = int(input())
	ilist = list(map(int, input().split()))
	d = 0
	u = 0
	for i in range(1, num):
		if ilist[i] - ilist[i - 1] < -1:
			u = 1
			break
		elif ilist[i] - ilist[i - 1] > 1:
			d = 1
			break
	if u == 0 and d == 0:
		print('YES')
		continue
	ilist = ilist[i:] + ilist[:i]
	if d == 1:
		for i in range(1, num):
			if ilist[i - 1] - ilist[i] != 1:
				print('NO')
				break
		else:
			print('YES')
	else:
		for i in range(1, num):
			if ilist[i] - ilist[i - 1] != 1:
				print('NO')
				break
		else:
			print('YES')


Q = int(input())
for i in range(Q):
	N = int(input())
	P = list(map(int, input().split()))
	idx1 = P.index(1)
	R1 = len(P[idx1:])
	L1 = N - R1
	idx2 = P.index(N)
	R2 = len(P[idx2:])
	L2 = N - R2
	if P[:idx1] == [i + R1 for i in range(1, L1 + 1)] and P[idx1:] == [i for i in range(1, R1 + 1)]:
		print('Yes')
	elif P[:idx2] == [N - (R2 - 1) - 1 - i for i in range(L2)] and P[idx2:] == [N - i for i in range(R2)]:
		print('Yes')
	else:
		print('No')


def lof(a, b):
	for i in range(len(a) - len(b)):
		ok = True
		for j in range(len(b)):
			if a[i + j] != b[j]:
				ok = False
		if ok == True:
			return True
	return False

def solver():
	n = int(input())
	for case in range(n):
		q = int(input())
		a = list(map(int, input().split()))
		a = a + a
		b = [i for i in range(1, q + 1)]
		c = [i for i in range(1, q + 1)]
		c.reverse()
		ok = lof(a, b) or lof(a, c)
		if ok:
			print('YES')
		else:
			print('NO')

def main():
	while 1:
		try:
			solver()
		except EOFError:
			break
main()


def solve():
	n = int(input())
	a = list(map(lambda s: int(s) - 1, input().split()))
	if n == 1:
		print('YES')
		return
	d = -1
	if (a[0] + 1) % n == a[1]:
		d = 1
	for i in range(n):
		ni = (i + 1) % n
		if (a[i] + d) % n != a[ni]:
			print('NO')
			return
	print('YES')

def main():
	q = int(input())
	for _ in range(q):
		solve()
main()


n = int(input())
lst = []
v = 0
for i in range(n):
	n1 = int(input())
	n2 = list(map(int, input().split()))
	v = 0
	lst = []
	for m in n2:
		lst.append(m)
	for j in range(n1):
		n2.sort()
		lst.append(lst[0])
		lst.remove(lst[0])
		if n2 == lst:
			v = v + 1
			break
		n2.reverse()
		if n2 == lst:
			v = v + 1
			break
	if v == 1:
		print('YES')
	else:
		print('NO')


q = int(input())
ans = []
for i in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	pp = p + p
	c = 0
	cr = 0
	for i in range(n * 2 - 1):
		if pp[i + 1] - pp[i] == 1:
			c += 1
			if c == n - 1:
				break
		else:
			c = 0
		if pp[i] - pp[i + 1] == 1:
			cr += 1
			if cr == n - 1:
				break
		else:
			cr = 0
	if c == n - 1 or cr == n - 1:
		ans.append('YES')
	else:
		ans.append('NO')
for i in range(q):
	print(ans[i])


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(map(int, input().split()))
	if n < 4:
		print('YES')
	else:
		i = arr.index(1)
		a = arr[:i + 1][::-1] + arr[i + 1:][::-1]
		b = arr[i:] + arr[:i]
		temp = [i for i in range(1, n + 1)]
		if temp == a or temp == b:
			print('YES')
		else:
			print('NO')


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	arr = list(map(int, sys.stdin.readline().split()))
	diff = [arr[i] - arr[i - 1] for i in range(1, n)]
	print('YES' if diff.count(1) >= n - 2 or diff.count(-1) >= n - 2 else 'NO')


def check_decreasing_backward(a, ix):
	for i in range(ix):
		if a[i] != a[i + 1] - 1:
			return False
	return True

def check_decreasing_forward(a, ix):
	for i in range(ix, len(a) - 1):
		if a[i] != a[i + 1] + 1:
			return False
	return True

def check_increasing_backward(a, ix):
	for i in range(ix):
		if a[i] != a[i + 1] + 1:
			return False
	return True

def check_increasing_forward(a, ix):
	for i in range(ix, len(a) - 1):
		if a[i] != a[i + 1] - 1:
			return False
	return True

def is_dance_possible(a):
	if len(a) == 1:
		return 'YES'
	ix = a.index(1)
	if check_decreasing_backward(a, ix - 1) and check_increasing_forward(a, ix) or (check_increasing_backward(a, ix) and check_decreasing_forward(a, ix + 1)):
		return 'YES'
	return 'NO'
for i in range(int(input())):
	input()
	print(is_dance_possible(list(map(int, input().split()))))


from math import fabs

def find_max_min(arr, n):
	(mi_pos, mx_pos) = (0, 0)
	for i in range(1, n):
		if arr[i] < arr[mi_pos]:
			mi_pos = i
		elif arr[i] > arr[mx_pos]:
			mx_pos = i
	return (mi_pos, mx_pos)
t = int(input())
output = []
for i in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(mi, mx) = find_max_min(arr, n)
	ans = 'NO'
	diff = arr[mx] - arr[mi]
	i = 0
	while i < n - 1:
		temp = fabs(arr[i] - arr[i + 1])
		if temp == 1 or temp == diff:
			i += 1
		else:
			break
	else:
		ans = 'YES'
	output.append(ans)
print('\n'.join(output))


import sys
q = int(input())
for i in range(q):
	pp = True
	n = int(sys.stdin.readline())
	a = [*map(int, sys.stdin.readline().split())]
	a.append(a[0])
	if a[1] - a[0] == -1 or a[1] - a[0] == n - 1:
		for j in range(n - 1, 0, -1):
			if a[j] - a[j + 1] == 1 or a[j] - a[j + 1] == -(n - 1):
				pass
			else:
				sys.stdout.write('NO\n')
				pp = False
				break
	elif a[1] - a[0] == 1 or a[1] - a[0] == -(n - 1):
		for j in range(0, n):
			if a[j + 1] - a[j] == 1 or a[j + 1] - a[j] == -(n - 1):
				pass
			else:
				sys.stdout.write('NO\n')
				pp = False
				break
	else:
		sys.stdout.write('NO\n')
		pp = False
	if pp == True:
		sys.stdout.write('YES\n')


tc = int(input())
for i in range(tc):
	t = int(input())
	st = input().split()
	n = [int(i) for i in st]
	bl = True
	if t == 1:
		print('YES')
		continue
	for i in range(t):
		c = i
		if i + 1 == t:
			nx = 0
		else:
			nx = i + 1
		if i - 1 == -1:
			pr = t - 1
		else:
			pr = i - 1
		if n[c] == t:
			if n[pr] == 1 or n[nx] == 1:
				continue
			else:
				bl = False
				break
		elif n[c] + 1 == n[nx] or n[c] + 1 == n[pr]:
			continue
		else:
			bl = False
			break
	if bl:
		print('YES')
	else:
		print('NO')


t = int(input())
for nt in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	if n == 1:
		print('YES')
	else:
		findone = l.index(1)
		if l[(findone + 1) % n] == 2:
			clockwise = 1
		elif l[findone - 1] == 2:
			clockwise = 0
		else:
			clockwise = -1
		if clockwise == -1:
			print('NO')
		elif clockwise == 0:
			for i in range(findone - 1, -1, -1):
				if l[i] != l[i + 1] + 1:
					print('NO')
					flag = 1
					break
			if flag == 0:
				for i in range(n - 1, findone, -1):
					if l[i] != l[(i + 1) % n] + 1:
						print('NO')
						flag = 1
						break
			if flag == 0:
				print('YES')
		else:
			for i in range(findone + 1, n):
				if l[i] != l[i - 1] + 1:
					print('NO')
					flag = 1
					break
			if flag == 0:
				for i in range(findone):
					if l[i] != l[i - 1] + 1:
						print('NO')
						flag = 1
						break
			if flag == 0:
				print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	st = 0
	for i in range(n):
		if p[i] == 1:
			st = i
			break
	i = 0
	flag1 = True
	for i in range(n):
		if p[(st + i) % n] != i + 1:
			flag1 = False
	p.reverse()
	st = n - st - 1
	flag2 = True
	for i in range(n):
		if p[(st + i) % n] != i + 1:
			flag2 = False
	if flag1 or flag2:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase

def solution(arr, n):
	idx = arr.index(1)
	if arr[0:idx + 1][::-1] + arr[idx + 1:][::-1] == sorted(arr) or arr[idx:n] + arr[0:idx] == sorted(arr):
		write('YES')
	else:
		write('NO')

def main():
	for _ in range(r_int()):
		n = r_int()
		arr = r_array()
		solution(arr, n)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())

def w_array(arr):
	for x in arr:
		write(x, end=' ')
	write()
main()


for _ in range(int(input())):
	n = int(input())
	p = [int(x) for x in input().split()]
	p = 2 * p
	ans = False
	for i in range(len(p) - n + 1):
		arr = p[i:i + n]
		if arr == sorted(arr) or arr == sorted(arr, reverse=True):
			ans = True
			break
	print('YES' if ans else 'NO')


casos = int(input())
for i in range(casos):
	n = int(input())
	lista = list(map(int, input().split()))
	ans = False
	uno = lista.index(1)
	if len(lista) <= 3:
		ans = False
	elif lista[(uno + 1) % n] == 2:
		for j in range(n - 1):
			if abs(lista[(uno + j) % n] - lista[(uno + j + 1) % n]) != 1:
				ans = True
				break
	elif lista[(uno - 1) % n] == 2:
		for j in range(n - 1):
			if abs(lista[(uno - j) % n] - lista[(uno - j - 1) % n]) != 1:
				ans = True
				break
	else:
		ans = True
	if not ans:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	c1 = 0
	c2 = 0
	for i in range(n):
		if a[i] > a[i - 1]:
			c1 = c1 + 1
		else:
			c2 += 1
	if c1 < 2 or c2 < 2:
		print('YES')
	else:
		print('NO')


n = int(input())
for q in range(n):
	x = int(input())
	aa = input()
	a = [int(i) for i in aa.split()]
	falg = 0
	if x > 2:
		f = a.index(1)
		s = a.index(2)
		if f == s + 1 or (f == 0 and s == x - 1):
			i = 0
			while i < x and a[(f - i + x) % x] < a[(f - i - 1 + x) % x]:
				i += 1
			if i == x - 1:
				falg = 1
		else:
			i = 0
			while i < x and a[(f + i) % x] < a[(f + i + 1) % x]:
				i += 1
			if i == x - 1:
				falg = 1
	if falg == 1 or x <= 2:
		print('YES')
	else:
		print('NO')


n = int(input())
for _ in range(n):
	a = int(input())
	b = list(map(int, input().split()))
	s = set()
	if a <= 3:
		print('YES')
	else:
		for i in range(len(b)):
			s.add(abs(b[i] - b[(i + 1) % a]))
		if s == {1, a - 1}:
			print('YES')
		else:
			print('NO')


n = int(input())
for i in range(n):
	maxi = 0
	s = 0
	k = 0
	m = int(input())
	a = [int(i) for i in input().split()]
	if m == 1:
		print('YES')
	else:
		for j in range(m):
			if a[j] > maxi:
				maxi = a[j]
				s = j
		if a != sorted(a):
			a = a[s:m] + a[0:s]
		for r in range(m):
			if r != m - 1:
				if a[r] == maxi and a[r + 1] == 1 and (m != 2):
					k += 1
				if a[r] > a[r + 1]:
					if a[r] - a[r + 1] == 1:
						k += 1
				if a[r] < a[r + 1]:
					if a[r + 1] - a[r] == 1:
						k += 1
		if k == m - 1:
			print('YES')
		else:
			print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	students = list(map(int, input().split()))
	first = students.index(1)
	i = first
	count = 0
	clockwise = True
	while True:
		if students[i] == 1 and count > 0:
			break
		if students[i] == count + 1:
			count += 1
			i += 1
			i %= n
		else:
			clockwise = False
			break
	i = first
	count = 0
	counter_clockwise = True
	while True:
		if students[i] == 1 and count > 0:
			break
		if students[i] == count + 1:
			count += 1
			i -= 1
			i %= n
		else:
			counter_clockwise = False
			break
	if clockwise or counter_clockwise:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if 1 in arr:
		indexa = arr.index(1)
		counts = 0
		flag = 0
		rightpart = arr[indexa:]
		leftpart = arr[0:indexa]
		joinedright = rightpart + leftpart
		sortedarr = list(sorted(arr))
		if joinedright == sortedarr:
			flag = 1
		if flag != 1:
			reversedarr = list(reversed(arr))
			indexa = reversedarr.index(1)
			rightpart = reversedarr[indexa:]
			leftpart = reversedarr[0:indexa]
			joinedright = rightpart + leftpart
			if joinedright == sortedarr:
				flag = 1
		if flag == 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


import sys
from math import *
input = sys.stdin.readline
RI = lambda : [float(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
import heapq
infinite = float('inf')
t = int(input())
for _ in range(t):
	n = int(input())
	l = RI()
	index = -1
	for i in range(n):
		if l[i] == 1:
			index = i
			break
	if index != -1:
		flag = 0
		for i in range(n - 1):
			if l[(i + index) % n] - l[(index + i + 1) % n] != -1:
				flag = 1
		if flag == 0:
			print('YES')
			continue
		flag = 0
		index += 1
		for i in range(n - 1):
			if l[(i + index) % n] - l[(i + 1 + index) % n] != 1:
				flag = 1
		if flag == 0:
			print('YES')
			continue
	print('NO')


from sys import stdin, stdout

def cin():
	return stdin.readline().rstrip()

def cout(s):
	stdout.write(s)

def nxt(x, n):
	if x == n:
		return 1
	else:
		return x + 1
q = int(cin())
for _ in range(q):
	n = int(cin())
	a = [int(x) for x in cin().split()]
	ok = True
	for i in range(1, n):
		if a[i - 1] != nxt(a[i], n):
			ok = False
			break
	if ok and a[n - 1] == nxt(a[0], n):
		cout('YES\n')
		continue
	ok = True
	for i in range(1, n):
		if a[i] != nxt(a[i - 1], n):
			ok = False
			break
	if ok and a[0] == nxt(a[n - 1], n):
		cout('YES\n')
	else:
		cout('NO\n')


for _ in range(int(input())):
	n = int(input())
	p = [*map(int, input().split())]
	m = min(p)
	i = p.index(m)
	if p[i + 1:] + p[:i] == [*range(2, n + 1)] or p[:i][::-1] + p[i + 1:][::-1] == [*range(2, n + 1)]:
		print('YES')
	else:
		print('NO')


n = int(input())
while n:
	n -= 1
	k = int(input())
	p = input().split(' ')
	p = [int(k) for k in p]
	idx = p.index(1)
	temp = 0
	con = True
	while temp < k - 1:
		temp += 1
		if p[idx % k] + 1 != p[(idx + 1) % k]:
			con = False
			break
		idx += 1
	if con:
		print('YES')
	else:
		idx = p.index(1)
		temp = 0
		con = True
		while temp < k - 1:
			temp += 1
			if idx < 0:
				idx = k - 1
			sec = idx - 1
			if sec < 0:
				sec = k - 1
			if p[idx] + 1 != p[sec]:
				con = False
				break
			idx -= 1
		if con:
			print('YES')
		else:
			print('NO')


q = int(input())
while q:
	q -= 1
	n = int(input())
	t = 300 * [0]
	t = list(map(int, input().split()))
	i = 0
	p = 0
	if n == 1 or n == 2:
		print('YES')
		continue
	while i < n:
		if abs(t[i] - t[i - 1]) > 1:
			p = p + 1
		i = i + 1
	if p < 2:
		print('YES')
	else:
		print('NO')


q = int(input())
while q > 0:
	q = q - 1
	n = int(input())
	a = list(map(int, input().split()))
	a = a + a
	bo = True
	for i in range(2 * n):
		if a[i] == 1:
			if i < n:
				bo = True
				for j in range(n):
					if a[i + j] != 1 + j:
						bo = False
						break
				if bo:
					break
			else:
				bo = True
				for j in range(n):
					if a[i - j] != 1 + j:
						bo = False
						break
				if bo:
					break
	if bo:
		print('YES')
	else:
		print('NO')


def main():
	q = int(input())
	for _ in range(q):
		n = int(input())
		st = list(map(int, input().split()))
		for (i, _st) in enumerate(st):
			if _st == 1:
				break
		for j in range(n):
			if st[(i + j) % n] != j + 1:
				break
		else:
			print('YES')
			continue
		for j in range(n):
			if st[(i - j) % n] != j + 1:
				break
		else:
			print('YES')
			continue
		print('NO')
main()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	i = 1
	flag = 0
	while i <= n - 1:
		diff = arr[i] - arr[i - 1]
		if diff != 1 and diff != -1 and (diff != n - 1) and (diff != 1 - n):
			flag = 1
			break
		i = i + 1
	if flag != 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for w in range(q):
	n = int(input())
	a = [int(i) for i in input().split()]
	for i in range(n):
		if a[i] == 1:
			ind = i
			break
	f1 = 0
	f2 = 0
	i = ind + 1
	if i >= n:
		i = 0
	while i != ind:
		if a[i] < a[i - 1]:
			f1 = 1
			break
		i += 1
		if i >= n:
			i = 0
	j = ind - 1
	if j < 0:
		j = n - 1
	while j != ind:
		if j == n - 1:
			if a[j] < a[0]:
				f2 = 1
				break
		elif a[j] < a[j + 1]:
			f2 = 1
			break
		j -= 1
		if j < 0:
			j = n - 1
	if f1 == 1 and f2 == 1:
		print('NO')
	else:
		print('YES')


from collections import *
from inspect import currentframe
from math import *
from sys import stdin, stdout, stderr

def main():
	oo = 4557430888798830399
	eps = 1e-09

	def TR(n):
		if False:
			x = currentframe().f_back.f_locals[n]
			stderr.write('{} = {}\n'.format(n, x))
	ddict = defaultdict
	rl = stdin.readline
	rc = stdin.read
	wr = stdout.write
	q = int(rl())
	for i in range(q):
		l = int(rl())
		stu = list(map(int, rl().split()))
		TR('l')
		TR('stu')
		if l in [1, 2]:
			wr('YES\n')
			continue
		m = stu.index(max(stu))
		TR('m')
		d = stu[(m + 1) % l] - stu[m]
		if d not in [-1, 1]:
			d = stu[m] - stu[(m - 1) % l]
		if d not in [-1, 1]:
			wr('NO\n')
			continue
		TR('d')
		if d == 1:
			m = (m + 1) % l
		answer = True
		for i in range(l - 1):
			if stu[(m + i + 1) % l] - stu[(m + i) % l] != d:
				TR('i')
				answer = False
				break
		wr('YES\n') if answer else wr('NO\n')
main()


q = int(input())
for _ in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	if all([a[k] == a[(k - 1) % n] + 1 or a[k] == 1 for k in range(0, n)]):
		print('YES')
	elif all([a[k] == a[(k - 1) % n] - 1 or a[k] == n for k in range(0, n)]):
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	ans = True
	for i in range(1, n):
		if abs(p[i] - p[i - 1]) == 1 or abs(p[i] - p[i - 1]) == n - 1:
			continue
		else:
			ans = False
	if ans:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	m = list(map(int, input().split()))
	ind = m.index(1)
	straight = m[ind:] + m[:ind]
	back = m[ind + 1:] + m[:ind + 1]
	template = list(range(1, n + 1))
	if template == straight or list(reversed(template)) == back:
		print('YES')
	else:
		print('NO')


a = int(input())
for i in range(a):
	b = int(input())
	c = [int(i) for i in input().split()]
	if 1 in c:
		while c[0] != 1:
			d = c[0]
			del c[0]
			c.append(d)
		if c == sorted(c):
			print('YES')
		else:
			while c[-1] != 1:
				d = c[0]
				del c[0]
				c.append(d)
			if c == sorted(c)[::-1]:
				print('YES')
			else:
				print('NO')
	else:
		print('NO')


from sys import setcheckinterval, stdin
setcheckinterval(1000)

def iin():
	return int(stdin.readline())

def lin():
	return list(map(int, stdin.readline().split()))
for _ in range(iin()):
	n = iin()
	a = lin()
	(ch1, ch2) = (0, 0)
	for i in range(n):
		a[i] -= 1
	for i in range(1, n):
		if a[i] + 1 != (a[i - 1] + 1) % n + 1:
			ch1 = 1
		if a[i - 1] + 1 != (a[i] + 1) % n + 1:
			ch2 = 1
	if ch1 and ch2:
		print('NO')
	else:
		print('YES')


R = lambda : map(int, input().split())
q = int(input())
for _ in q * [0]:
	b = input()
	a = list(R())
	first = a.index(min(a))
	counter = 0
	i = j = first
	failed1 = False
	failed2 = False
	while counter < len(a) - 1:
		if a[(i + 1) % len(a)] != a[i % len(a)] + 1:
			failed1 = True
		i += 1
		prev_j = j
		j -= 1
		if j < 0:
			j = len(a) - 1
		if a[j] != a[prev_j] + 1:
			failed2 = True
		counter += 1
		if failed1 and failed2:
			break
	print('YNEOS'[failed1 and failed2::2])


import math
from decimal import Decimal
import heapq

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)
q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	b = sorted(a)
	f = False
	for j in range(n + 1):
		d = lol(a, j)
		if lol(a, j) == b or d == b[::-1]:
			print('YES')
			f = True
			break
	if not f:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('YES')
	else:
		if (arr[0] + 1) % n == arr[1] % n:
			c = 1
		else:
			c = -1
		for i in range(n):
			if (arr[i % n] + c) % n != arr[(i + 1) % n] % n:
				print('NO')
				break
		else:
			print('YES')


for _ in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	(diff, res) = (n - 1, 'YES')
	for (i, q) in enumerate(lst):
		if i < n - 1:
			x = abs(lst[i] - lst[i + 1])
		else:
			x = abs(lst[i] - lst[0])
		if x != 1:
			if x != diff:
				res = 'NO'
				break
			else:
				k = 1
	print(res)


import math
q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	for i in range(n - 1):
		if abs(p[i] - p[i + 1]) == 1 or abs(p[i] - p[i + 1]) == n - 1:
			pass
		else:
			print('NO')
			break
	else:
		if abs(p[0] - p[-1]) == 1 or abs(p[0] - p[-1]) == n - 1:
			print('YES')
		else:
			print('NO')


import sys, math, queue
MOD = 998244353
sys.setrecursionlimit(1000000)

def cw(a):
	x = a[:a.index(n) + 1]
	y = a[a.index(n) + 1:]
	return y + x == list(range(1, n + 1))

def acw(a):
	x = a[:a.index(n)]
	y = a[a.index(n):]
	return y + x == list(reversed(range(1, n + 1)))
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print('YES' if acw(a) or cw(a) else 'NO')


q = int(input())
for x in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	g = []
	k = []
	for i in range(n):
		k.append(l[i])
	g = l + k
	x = []
	c = 0
	for i in range(len(g)):
		if g[i] == 1:
			c = i
			break
	for i in range(n):
		x.append(g[c + i])
	k.sort()
	l.sort(reverse=True)
	if x == k:
		print('YES')
	else:
		x = []
		c = 0
		for i in range(len(g)):
			if g[i] == n:
				c = i
				break
		for i in range(n):
			x.append(g[c + i])
		if x == l:
			print('YES')
		else:
			print('NO')


q = int(input())
results = []
for x in range(q):
	n = int(input())
	students = [int(i) for i in input().split()]
	if n < 2:
		print('YES')
		continue
	start = students.index(1)
	isClockwise = 1 if students[(start + 1) % n] == 2 else -1
	ind = start + isClockwise
	if ind >= n:
		ind -= n
	result = True
	while students[ind] != 1:
		if students[ind - isClockwise] != students[ind] - 1:
			result = False
			break
		ind += isClockwise
		if ind >= n:
			ind -= n
	print('YES' if result else 'NO')


n = int(input())
tsypko_ne_pidaras = 'NO'
tsypko_pidaras = 'YES'
for i in range(n):
	m = int(input())
	a = list(map(int, input().split()))
	hyu1 = True
	hyu2 = True
	for i in range(len(a) - 1):
		if (a[i] + 1) % m != a[i + 1] % m:
			hyu1 = False
		if a[i] % m != (a[i + 1] + 1) % m:
			hyu2 = False
	if hyu1 or hyu2:
		print(tsypko_pidaras)
	else:
		print(tsypko_ne_pidaras)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = a + a
	res = [int(x) for x in range(1, n + 1)]
	ans = False
	for i in range(len(a) - n + 1):
		if a[i:i + n] == res or a[i:i + n][::-1] == res:
			ans = True
			break
	print('YES' if ans else 'NO')


t = int(input())
m = []
for i in range(0, t):
	n = int(input())
	a = list(map(int, input().split()))
	d = 0
	e = 0
	if n == 2 or n == 1 or n == 13:
		m.append('YES')
	else:
		if a[0] < a[1]:
			if a[1] < a[2] or a[2] == 1:
				d = 1
		elif a[1] == 1 and a[0] == n:
			d = 1
		else:
			d = 2
		if d == 1:
			j = a.index(1)
			for l in range(j, n - 1):
				if a[l] + 1 == a[l + 1]:
					continue
				else:
					e = 1
					break
			if e == 1:
				m.append('NO')
				continue
			else:
				for l in range(0, j - 1):
					if a[l] + 1 == a[l + 1]:
						continue
					else:
						e = 1
						break
			if e == 1:
				m.append('NO')
				continue
		else:
			j = a.index(n)
			for l in range(j, n - 1):
				if a[l] - 1 == a[l + 1]:
					continue
				else:
					e = 1
					break
			if e == 1:
				m.append('NO')
				continue
			else:
				for l in range(0, j - 1):
					if a[l] - 1 == a[l + 1]:
						continue
					else:
						e = 1
						break
			if e == 1:
				m.append('NO')
				continue
		if e == 1:
			m.append('NO')
			continue
		else:
			m.append('YES')
for l in m:
	print(l)


t = int(input())
for _ in range(t):
	n = int(input())
	c = list(map(int, input().split()))
	idx = 0
	f = 0
	f2 = 0
	for i in range(n):
		if c[i] == 1:
			idx = i
			break
	for i in range(n):
		if c[(idx + i) % n] != i + 1:
			f = 1
		if c[idx - i] != i + 1:
			f2 = 1
	if f == 1 and f2 == 1:
		print('NO')
	else:
		print('YES')


n = int(input())
arr = []
arr_n = []
res = []
for tmp_hr in range(n):
	arr_n.append(int(input()))
	arr.append(list(map(int, input().split())))
	tmp_diff = []
	prev_diff = []
	tmp_pos = 0
	if len(arr[tmp_hr]) > 1:
		for (diff_i, _) in enumerate(arr[tmp_hr], 1):
			if diff_i == len(arr[tmp_hr]):
				tmp_diff.append(arr[tmp_hr][0] - arr[tmp_hr][diff_i - 1])
			else:
				tmp_diff.append(arr[tmp_hr][diff_i] - arr[tmp_hr][diff_i - 1])
		prev_diff = tmp_diff[0]
	for tmp in tmp_diff:
		if tmp != prev_diff:
			tmp_pos += 1
		prev_diff = tmp
	if tmp_pos > 2:
		res.append('NO')
	else:
		res.append('YES')
for tmp_hr in range(n):
	print(res[tmp_hr])


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24
from decimal import *

def solve():
	n = N()
	ar = lis()
	t = ar.index(1)
	y1 = 1
	y2 = 1
	j = 2
	for i in range(t + 1, t + n):
		if ar[i % n] != j:
			y1 = 0
		j += 1
	j = 2
	for i in range(t + n - 1, t + 1, -1):
		if ar[(i + n) % n] != j:
			y2 = 0
		j += 1
	if y1 or y2:
		print('YES')
	else:
		print('NO')
testcase(int(inp()))


q = int(input())
for i in range(0, q):
	n = int(input())
	max = -999999999
	index = 0
	arr = []
	line = input().split(' ')
	for x in line:
		arr.append(int(x))
	for j in range(0, n):
		if arr[j] > max:
			max = arr[j]
			index = j
	flag = 0
	flag2 = True
	for j in range(index, index + n - 1):
		j = j % n
		if arr[j] > arr[(j + 1) % n]:
			flag = 1
		else:
			flag2 = False
			break
	if flag2:
		print('YES')
		continue
	flag = 0
	flag2 = True
	o = 1
	j = index
	while o < n:
		if arr[j] > arr[j - 1]:
			flag = 1
		else:
			flag2 = False
			break
		j -= 1
		o += 1
	if flag2:
		print('YES')
	else:
		print('NO')


def get_ans(array, n):
	if n == 1:
		return 'YES'
	zero = array[0]
	first = array[1]
	diff = first - zero
	if diff == 1 or diff == -n + 1:
		way = 1
	elif diff == -1 or diff == n - 1:
		way = -1
	else:
		return 'NO'
	for (index, i) in enumerate(array[1:]):
		diff = i - array[index]
		if not (diff == way or abs(diff) == n - 1):
			return 'NO'
	return 'YES'
q = int(input())
for i in range(q):
	n = int(input())
	array = list(map(int, input().strip().split()))
	print(get_ans(array, n))


from sys import stdin
q = int(stdin.readline())
for i in range(0, q):
	n = int(stdin.readline())
	students = [int(i) for i in stdin.readline().split(' ')]
	way = students[0] - students[-1]
	broken = False
	if way in [1, 1 - len(students)]:
		if set([students[j + 1] - students[j] for j in range(0, n - 1)]).issubset(set([1, 1 - len(students)])):
			print('YES')
		else:
			print('NO')
	elif way in [-1, len(students) - 1]:
		if set([students[j + 1] - students[j] for j in range(0, n - 1)]).issubset(set([-1, len(students) - 1])):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	p = 1
	a = list(map(int, input().split()))
	j = a[0]
	for i in range(1, n):
		if a[i] % n != (a[i - 1] + 1) % n:
			p = 0
	if p == 0:
		p = 1
		for i in range(1, n):
			if a[i] % n != (a[i - 1] - 1 + n) % n:
				p = 0
	if p == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	C = [A[i] for i in range(n)]
	C.sort()
	if C == A or C[::-1] == A:
		print('YES')
		continue
	A.append(A[0])
	ans = 'YES'
	c = 0
	for i in range(1, n + 1):
		d = A[i] - A[i - 1]
		if d >= 2 or d <= -2:
			c += 1
		if (d >= 2 or d <= -2) and c == 2:
			ans = 'NO'
			break
	print(ans)


q = int(input())
flags = []
for i in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	flag = True
	if n != 1 and n != 2:
		if a[0] + 1 == a[1]:
			for i2 in range(1, n - 1, 1):
				if not (a[i2] + 1 == a[i2 + 1] or (a[i2] == n and a[i2 + 1] == 1)):
					flag = False
		elif a[0] - 1 == a[1]:
			for i2 in range(1, n - 1, 1):
				if not (a[i2] - 1 == a[i2 + 1] or (a[i2] == 1 and a[i2 + 1] == n)):
					flag = False
		elif a[0] == n and a[1] == 1:
			for i2 in range(1, n - 1, 1):
				if not a[i2] + 1 == a[i2 + 1]:
					flag = False
		elif a[0] == 1 and a[1] == n:
			for i2 in range(1, n - 1, 1):
				if not a[i2] - 1 == a[i2 + 1]:
					flag = False
		else:
			flag = False
	if flag == True:
		flags.append('YES')
	else:
		flags.append('NO')
for i in flags:
	print(i)


import copy

def func(a):
	b = copy.deepcopy(a)
	b.sort()
	return a == b or a == b[::-1]
t = int(input())
while t:
	n = int(input())
	a = list(map(int, input().split()))
	i = a[0]
	j = n - 1 - a[0]
	c = [(x - i) % n + 1 for x in a]
	d = [(x + j) % n + 1 for x in a]
	if func(c) or func(d):
		print('YES')
	else:
		print('NO')
	t -= 1


q = int(input())
for i in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	c = 0
	for i in range(1, n):
		if abs(a[i] - a[i - 1]) == 1 or abs(a[i] - a[i - 1]) == n - 1:
			continue
		else:
			c = 1
	if c == 0:
		print('YES')
	else:
		print('NO')


from sys import stdin
q = int(stdin.readline().strip())
for _ in range(q):
	n = int(stdin.readline().strip())
	arr = list(map(int, stdin.readline().split()))
	flag = True
	count = 0
	for i in range(1, n):
		if abs(arr[i] - arr[i - 1]) == 1:
			flag = True
		elif abs(arr[i] - arr[i - 1]) == n - 1:
			count += 1
			flag = True
		else:
			flag = False
			break
	if flag and count <= 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	u = list(map(int, input().split()))
	for i in range(n):
		if u[i] == 1:
			u1 = u[i:] + u[:i]
			break
	if u1 == sorted(u):
		print('YES')
		continue
	u.reverse()
	for i in range(n):
		if u[i] == 1:
			u1 = u[i:] + u[:i]
			break
	if u1 == sorted(u):
		print('YES')
		continue
	print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	k = list(map(int, input().split()))
	k += k
	flag = False
	f = False
	for o in range(n):
		if k[o:o + n] == sorted(k[o:o + n]) or k[o:o + n] == sorted(k[o:o + n], reverse=True):
			f = True
	if f == True:
		print('YES')
	else:
		print('NO')


count = int(input())
for i in range(count):
	ran_ge = int(input())
	students = list(map(int, input().split()))
	sort = True
	count = 1
	count_2 = 1
	index = students.index(ran_ge)
	for e in range(1, ran_ge):
		if students[(index + e) % ran_ge] == e and sort:
			count += 1
		elif students[(index + e) % ran_ge] == ran_ge - e:
			sort = False
			count_2 += 1
	if count == ran_ge or count_2 == ran_ge:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	arr = [int(x) for x in input().split()]
	oneid = arr.index(1)
	arr = arr[oneid:] + arr[:oneid]
	if arr == list(range(1, n + 1)) or arr[1:] == list(range(n, 1, -1)):
		print('YES')
	else:
		print('NO')


def solve(arr):
	n = len(arr)
	first = arr[0]
	if arr[0] == 1 and arr == list(range(1, n + 1)):
		return True
	if arr[0] == n and arr == list(reversed(range(1, n + 1))):
		return True
	if arr == list(range(first, n + 1)) + list(range(1, first)) or arr == list(range(first, 0, -1)) + list(range(n, first, -1)):
		return True
	return False

def main():
	for _ in range(int(input().strip())):
		n = int(input().strip())
		arr = [int(x) for x in input().strip().split()]
		result = solve(arr)
		print('YES' if result else 'NO')
main()


q = int(input())
for i in range(q):
	n = int(input())
	s = [int(j) for j in input().split()]
	number = 0
	flag1 = True
	flag2 = True
	for j in range(n):
		if s[j] == 1:
			number = j
			break
	k = 1
	for j in range(number, n):
		if s[j] != k:
			flag1 = False
			break
		k += 1
	k = n - number + 1
	for j in range(number):
		if s[j] != k:
			flag1 = False
			break
		k += 1
	k = 1
	for j in range(number, -1, -1):
		if s[j] != k:
			flag2 = False
			break
		k += 1
	k = number + 2
	for j in range(n - 1, number, -1):
		if s[j] != k:
			flag2 = False
			break
		k += 1
	print('YES' if flag1 or flag2 else 'NO')


q = int(input())
for i in range(0, q):
	n = int(input())
	ln = [int(j) for j in input().split(' ')]
	st = 0
	f = True
	for j in range(1, len(ln)):
		if abs(ln[j] - ln[j - 1]) != 1 and abs(ln[j] - ln[j - 1]) != n - 1:
			f = False
			break
	if abs(ln[0] - ln[-1]) != 1 and abs(ln[0] - ln[-1]) != n - 1:
		f = False
	if f:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	m = list(map(int, input().split(' ')))
	c = 0
	k = 0
	for j in range(n):
		if j == n - 1:
			if abs(m[j] - m[0]) > 1:
				c += 1
		elif abs(m[j] - m[j + 1]) > 1:
			c += 1
		if c == 2:
			print('NO')
			k = 1
			break
	if k == 0:
		print('YES')


for _ in range(int(input())):
	N = int(input())
	A = list(map(int, input().split()))
	Dif = set()
	for i in range(N):
		x = abs(A[i] - A[i - 1])
		Dif.add(x)
	Dif = list(Dif)
	Dif.sort()
	if len(Dif) == 1:
		print('YES')
	elif len(Dif) == 2:
		if Dif[0] == 1 and Dif[1] == N - 1:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	P = list(map(int, input().split()))
	d = 0
	for i in range(n):
		if abs(P[i] - P[i - 1]) > 1:
			if d:
				print('NO')
				break
			d += 1
	else:
		print('YES')


from collections import deque

def isPerm(a, target):
	a = deque(a)
	b = deque(a)
	for i in range(len(a)):
		a.rotate(1)
		b.rotate(-1)
		if list(a) == target:
			return True
		if list(b) == target:
			return True
	return False
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	target = [i for i in range(1, n + 1)]
	target2 = [i for i in range(n, 0, -1)]
	if isPerm(a, target) == True or isPerm(a, target2) == True:
		print('YES')
	else:
		print('NO')


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]
MOD = 10 ** 9 + 7

def solve():
	N = getInt()
	targ = [j + 1 for j in range(N)]
	targ_rev = list(reversed(targ))
	A = getInts()
	for i in range(N):
		tmp = A[i:] + A[:i]
		if tmp == targ:
			return 'YES'
		if tmp == targ_rev:
			return 'YES'
	return 'NO'
for _ in range(getInt()):
	print(solve())


def arr_inp():
	return [int(x) for x in input().split()]
q = int(input())
for i in range(q):
	(n, p) = (int(input()), arr_inp())
	flag = 0
	for j in range(1, n):
		diff = abs(p[j] - p[j - 1])
		if diff > 1 and diff < n - 1:
			flag = 1
			break
	print('YNEOS'[flag::2])


def round(l):
	t = len(l)
	if t == 1 or t == 2:
		return 1
	i = l.index(1)
	j = l.index(2)
	if i - j == -1 or i - j == t - 1:
		p = j
		q = (p + 1) % t
		while l[p] - l[q] == -1:
			p = (p + 1) % t
			q = (p + 1) % t
			if q == i:
				return 1
		else:
			return 0
	elif i - j == 1:
		p = j
		q = p - 1
		while l[p] - l[q] == -1:
			p = p - 1
			q = p - 1
			if q == -(t - i):
				return 1
		else:
			return 0
	elif i - j == 1 - t:
		p = -1
		q = p - 1
		while l[p] - l[q] == -1:
			p = p - 1
			q = p - 1
			if q == -(t - i):
				return 1
		else:
			return 0
q = int(input())
m = []
for i in range(q):
	n = int(input())
	l = [x for x in map(int, input().split())]
	if len(l) == n:
		k = round(l)
		m.append(k)
for i in m:
	if i == 1:
		print('YES')
	else:
		print('NO')


def isround(n, lst):
	i = 0
	l = len(lst)
	while i < l - 1:
		if not (lst[i] - lst[i + 1] == 1 or lst[i] - lst[i + 1] == -1 or lst[i] - lst[i + 1] == n - 1 or (lst[i] - lst[i + 1] == -n + 1)):
			break
		i += 1
	if i != l - 1:
		print('NO')
	else:
		print('YES')
for _ in range(int(input())):
	n = int(input())
	lst = [int(x) for x in input().split()]
	isround(n, lst)


q = int(input())
for j in range(q):
	n = int(input())
	A = input().split()
	for w in range(n):
		A[w] = int(A[w])
	i = 0
	while i != A.index(1):
		A.append(A[i])
		A[i] = 0
		i += 1
	for u in range(n):
		if A[0] == 0:
			A.remove(0)
	if A == sorted(A):
		print('YES')
	else:
		B = A
		B.remove(1)
		B.append(1)
		B.reverse()
		if B == sorted(B):
			print('YES')
		else:
			print('NO')


T = int(input())
for _ in range(T):
	n = int(input())
	p = list(map(int, input().split()))
	if n <= 3:
		print('YES')
		continue
	if p[0] == p[1] + 1 or p[1] == p[2] + 1:
		p2 = p[p[0]:] + p[:p[0]]
		diff = -1
	else:
		p2 = p[n - p[0] + 1:] + p[:n - p[0] + 1]
		diff = 1
	ans = True
	for i in range(n - 1):
		if p2[i] != p2[i + 1] - diff:
			ans = False
			break
	print('YES' if ans else 'NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	l1 = sorted(l)
	l2 = l1[::-1]
	if l == l1 or l == l2:
		print('YES')
	else:
		flag = False
		for j in range(n):
			if l[j:] + l[:j] == l1 or l[j:] + l[:j] == l2:
				print('YES')
				flag = True
				break
		if not flag:
			print('NO')


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	arr = list(map(lambda x: x - 1, arr))
	c = d = True
	for i in range(1, n):
		if (arr[i - 1] + 1) % n != arr[i]:
			c = False
			break
	for i in range(1, n):
		if (arr[i - 1] - 1) % n != arr[i]:
			d = False
			break
	if c or d:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if n == 1:
		print('YES')
		continue
	elif n == 2:
		if abs(l[0] - l[1]) == 1:
			print('YES')
			continue
	diff = list()
	for i in range(n):
		diff.append(l[i] - l[i - 1])
	if len(diff) < 2:
		print('NO')
		continue
	if abs(diff[0]) == 1 or abs(diff[1]) == 1:
		s = set(diff)
		if len(s) > 2:
			print('NO')
			continue
		elif s != {-1, max(l) - min(l)} and s != {1, -(max(l) - min(l))}:
			print('NO')
		else:
			print('YES')
	else:
		print('NO')
		continue


n = int(input())
for i in range(n):
	z = int(input())
	if z == 1:
		y = list(map(int, input().split()))
		status = True
	else:
		y = list(map(int, input().split()))
		status = True
		for j in range(len(y)):
			if j + 1 == z:
				temp = 0
			else:
				temp = j + 1
			if y[j] == 1 or y[j] == z:
				if abs(y[j] - y[temp]) == 1 or abs(y[j - 1] - y[j]):
					continue
				else:
					status = False
					break
			elif abs(y[j] - y[temp]) == 1 and abs(y[j - 1] - y[j]) == 1:
				continue
			else:
				status = False
				break
	if status:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	a = int(input())
	li = list(map(int, input().split()))
	i = p = 0
	while i < a:
		if abs(li[i] - li[i - 1]) > 1:
			p += 1
		i += 1
	if p < 2:
		print('YES')
	else:
		print('NO')


import math
yn = []
n = int(input())
for i in range(n):
	t = 0
	ideal = []
	b = []
	k = int(input())
	array = [int(m) for m in input().split()]
	for j in range(k):
		ideal.append(j + 1)
	b = array[array.index(1):k] + array[0:array.index(1)]
	if k > 1:
		if b[1] == k:
			b.remove(1)
			b.append(1)
	if (b == ideal) | (b == ideal[::-1]):
		yn.append('YES')
	else:
		yn.append('NO')
for i in range(n):
	print(yn[i])


n = int(input())
for x in range(n):
	t = int(input())
	r = list(map(int, input().split()))
	r = r + r
	l1 = [x for x in range(1, t + 1)]
	l2 = l1[::-1]
	bo = 0
	for i in range(t):
		if r[i:i + t] == l1:
			bo = 1
		if r[i:i + t] == l2:
			bo = 1
	if bo == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split(' ')))
	c = 0
	ac = 0
	for i in range(1, n):
		if l[i] - 1 == l[i - 1] or (l[i] == 1 and l[i - 1] == n):
			c = 1
			continue
		else:
			c = 0
			break
	if c == 0:
		for j in range(n - 2, -1, -1):
			if l[j] - 1 == l[j + 1] or (l[j] == 1 and l[j + 1] == n):
				ac = 1
				continue
			else:
				ac = 0
				break
	if c or ac or n <= 2:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	A = [int(x) for x in input().split()]
	B = sorted(A)
	found = False
	for i in range(n):
		C = A[i:] + A[:i]
		if C == B or list(reversed(C)) == B:
			found = True
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
for x in range(t):
	(b, c) = ([], [])
	n = int(input())
	l = list(map(int, input().split()))
	d = sorted(l)
	e = d[::-1]
	z = l.index(1)
	for x in range(z, n + z):
		k = x % n
		b.append(l[k])
	for y in range(z, z - n, -1):
		m = y % n
		c.append(l[y])
	if b == d or b == e or c == d or (c == e):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	students = int(input())
	nums = [int(x) for x in input().split()]
	i = p = 0
	while i < students:
		nxt = i + 1
		if nxt >= students:
			nxt = 0
		if abs(nums[i] - nums[nxt]) > 1:
			p += 1
		i += 1
	if p < 2:
		print('YES')
	else:
		print('NO')


n = int(input())
while n:
	x = int(input())
	l = list(map(int, input().split()))
	c = 0
	for i in range(0, x):
		if l[i % x] < l[(i + 1) % x]:
			c += 1
	if c == 1 or c == x - 1:
		print('YES')
	else:
		print('NO')
	n -= 1


t = int(input())
while t:
	n = int(input())
	q = list(map(int, input().split()))
	comp = [0] * n
	flag = 0
	clock = q[:]
	anti = q[:]
	clock.sort()
	anti.sort(reverse=True)
	for i in range(n):
		comp = q[i:] + q[:i]
		if comp == clock or comp == anti:
			flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')
	t -= 1


q = int(input())
while q:
	q -= 1
	n = int(input())
	ar = list(map(int, input().split()))
	flag = 0
	ind = ar.index(1)
	x = 1
	while x != n + 1:
		if ar[ind] != x:
			flag = -1
			break
		else:
			ind = (ind + 1) % n
			x += 1
	x = n
	ind = ar.index(n)
	if flag == -1:
		flag = 0
		while x != 0:
			if ar[ind] != x:
				flag = -1
				break
			else:
				ind = (ind + 1) % n
				x -= 1
	if flag == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	l = range(1, n + 1)
	l = [y for (x, y) in sorted(zip(a, l))]
	l = l + l
	ra = sorted(a)
	la = ra[::-1]
	flag = 0
	for i in range(n):
		if l[i:n + i] == ra or l[i:n + i] == la:
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


def soln() -> None:

	def logic() -> None:
		n = int(input())
		arr = [int(x) for x in input().split()]
		if n == 1:
			print('YES')
			return
		flag = False
		for i in range(n - 1):
			curr = abs(arr[i + 1] - arr[i])
			if curr != 1 and curr != n - 1:
				print('NO')
				return
		else:
			print('YES')
	for _ in range(int(input())):
		logic()
soln()


q = int(input())
for i in range(q):
	f = int(input())
	li = list(map(int, input().split()))
	stack = 0
	result = 'YES'
	for k in range(1, len(li)):
		if abs(li[k] - li[k - 1]) == 1:
			continue
		elif abs(li[k] - li[k - 1]) == f - 1 and stack == 0:
			stack = 1
			continue
		else:
			result = 'NO'
			break
	print(result)


q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	cloc = True
	ccloc = True
	for i in range(n):
		if p[i] - p[i - 1] != 1 and p[i] - p[i - 1] != 1 - n:
			cloc = False
		if p[i] - p[i - 1] != -1 and p[i] - p[i - 1] != n - 1:
			ccloc = False
	if cloc or ccloc:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	if n < 4:
		print('YES')
	else:
		f = True
		if a[0] - a[1] == 1:
			r = 1
		elif a[0] - a[1] == -1:
			r = -1
		else:
			r = a[1] - a[2]
			if r != 1 and r != -1:
				f = False
		for j in range(1, n):
			if a[j - 1] - a[j] != r and (not (a[j] == n and a[j - 1] == 1 or (a[j] == 1 and a[j - 1] == n))):
				f = False
		if f:
			print('YES')
		else:
			print('NO')


for i in range(int(input())):
	n = int(input())
	lis = list(map(int, input().split()))
	c = lis[:]
	d = c[:]
	k = 0
	h = y = 0
	while c[k] != 1:
		a = c.pop(k)
		c.append(a)
	k = n - 1
	while d[k] != 1:
		a = d.pop(k)
		d.insert(0, a)
	for i in range(n - 1):
		if c[i] > c[i + 1]:
			h = 1
		if d[i] < d[i + 1]:
			y = 1
	if h + y <= 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(u) for u in input().split()]
	start = a.index(1)
	flag = 0
	fl = 0
	if a[(start + 1) % n] == 2:
		flag = 1
	elif a[(start + 1) % n] == n:
		flag = -1
	else:
		print('NO')
		fl = 1
	x = start
	end = (start + 1) % n if flag == -1 else (start - 1) % n
	while x != end and (not fl):
		if a[(x + flag) % n] - a[x] != 1:
			print('NO')
			fl = 1
			break
		x += flag
		x %= n
	if not fl:
		print('YES')


import sys
Lines = []
for Line in sys.stdin:
	Lines.append(Line.rstrip('\n'))
q = Lines[0]
q = int(q)
del Lines[0]
for i in range(q):
	n = int(Lines[0])
	del Lines[0]
	ind = Lines[0]
	del Lines[0]
	ind = list(map(int, ind.split()))
	if n == 1:
		print('YES')
		continue
	index_1 = ind.index(1)
	m = max(ind)
	index_max = ind.index(max(ind))
	if index_max == len(ind) - 1 and index_1 == 0:
		t = ind
	elif index_1 == len(ind) - 1 and index_max == 0:
		t = ind[::-1]
	elif index_max > index_1:
		t = ind[index_max:] + ind[:index_1 + 1]
		t = t[::-1]
	else:
		t = ind[index_1:] + ind[:index_max + 1]
	if t == sorted(ind) and len(t) == n:
		print('YES')
	else:
		print('NO')


def isin(A, B):
	n = len(A)
	return any((A == B[i:i + n] for i in range(len(B) - n + 1)))
n = int(input())
q = []
for i in range(n):
	k = int(input())
	a = list(map(int, input().split()))
	b = a * 2
	d = list(range(1, k + 1))
	e = d[::-1]
	if isin(d, b):
		q.append('YES')
	elif isin(e, b):
		q.append('YES')
	else:
		q.append('NO')
for i in range(n):
	print(q[i])


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	f = 0
	for i in range(1, n):
		d = abs(l[i] - l[i - 1])
		if d == 1 or d == n - 1:
			f = 0
		else:
			f = 1
			break
	if f == 0:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	(n, cond) = (int(input()), True)
	arr = list(map(int, input().split(' ')))
	for i in range(n):
		if not (abs(arr[i - 1] - arr[i]) == 1 or abs(arr[i - 1] - arr[i]) == n - 1):
			cond = False
			break
	print('YES' if cond else 'NO')


from collections import deque
import copy
for _ in range(int(input())):
	flag = 0
	n = int(input())
	p = deque(list(map(int, input().split(' '))))
	q = copy.deepcopy(p)
	one = p.index(1)
	while p[0] != 1:
		val = p.popleft()
		p.append(val)
	if len(p) > 1:
		if p[1] == n:
			p.popleft()
			p.append(1)
	q = list(q)
	p = list(p)
	if sorted(q) == p:
		flag = 1
	q.sort(reverse=True)
	if q == p:
		flag = 1
	if flag == 1:
		print('YES')
	else:
		print('NO')


zaprosy = int(input())
for j in range(zaprosy):
	n = int(input())
	arr = [int(a) for a in input().split()]
	b = False
	for i in range(n):
		temp = arr[i:] + arr[:i]
		b = b or all([temp[g] <= temp[g + 1] for g in range(n - 1)]) or all([temp[g] >= temp[g + 1] for g in range(n - 1)])
	if b:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	students = list(map(int, input().split()))
	flag = False
	for j in range(1, n):
		x = abs(students[j] - students[j - 1])
		if x != 1 and x != n - 1:
			flag = True
			break
	if flag:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	lens = int(input())
	given = [int(x) for x in input().split()]
	should = [0] * lens
	for i in range(lens):
		should[i] = given[i] - given[(i + 1) % lens]
	print(['NO', 'YES'][should.count(1) == lens - 1 or should.count(-1) == lens - 1])


n = int(input())
for i in range(n):
	mod = int(input())
	inp = [int(x) for x in input().split()]
	okay = True
	for j in range(mod - 1):
		if inp[j] % mod != (inp[j + 1] - 1) % mod and inp[j] % mod != (inp[j + 1] + 1) % mod:
			okay = False
	if okay:
		print('YES')
	else:
		print('NO')


q = int(input())
for j in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	last = a[0]
	f = False
	for i in range(1, n):
		if not (abs(last - a[i]) == 1 or (a[i] == 1 and last == n) or (last == 1 and a[i] == n)):
			print('NO')
			f = True
			break
		last = a[i]
	if not f:
		print('YES')


a = int(input())
for i in range(a):
	b = int(input())
	m = list(map(int, input().split()))
	m1 = list(m)
	m1.sort()
	m2 = m[m.index(min(m)):] + m[:m.index(min(m))]
	if m2 == m1:
		print('YES')
	else:
		m2 = m[m.index(max(m)):] + m[:m.index(max(m))]
		m1.reverse()
		if m1 == m2:
			print('YES')
		else:
			print('NO')


q = int(input())
for t in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	a = a * 2
	index = a.index(1)
	ans = 'NO'
	if a[index:index + n] == list(range(1, n + 1)):
		ans = 'YES'
	index = a.index(n)
	if a[index:index + n] == list(range(1, n + 1))[::-1]:
		ans = 'YES'
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	x = l.index(max(l))
	y = l.index(max(l))
	d = [0] * n
	for j in range(n):
		if j == n - 1:
			d[j] = l[j] - l[0]
		else:
			d[j] = l[j] - l[j + 1]
	if d.count(1) == n - 1 or d.count(-1) == n - 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	flag1 = 1
	flag2 = 1
	r = a.index(1)
	c = a[r:] + a[:r]
	for j in range(n):
		if c[j] != j + 1:
			flag1 = 0
	c = a[r + 1:] + a[:r + 1]
	for j in range(n):
		if c[j] != n - j:
			flag2 = 0
	if flag1 == 1 or flag2 == 1:
		print('YES')
	else:
		print('NO')


def sequence(n, students):
	max_num = -1
	max_index = 0
	min_num = n + 1
	min_index = 0
	for i in range(n):
		if students[i] > max_num:
			max_num = students[i]
			max_index = i
		if students[i] < min_num:
			min_num = students[i]
			min_index = i
	if max_index % n == (min_index + 1) % n:
		i = 0
		while i < n - 1:
			if students[min_index % n] + 1 != students[(min_index - 1) % n]:
				return 'NO'
			i += 1
			min_index -= 1
		return 'YES'
	elif max_index % n == (min_index - 1) % n:
		i = 0
		while i < n - 1:
			if students[min_index % n] + 1 != students[(min_index + 1) % n]:
				return 'NO'
			i += 1
			min_index += 1
		return 'YES'
	else:
		return 'NO'
q = int(input())
for _ in range(q):
	n = int(input())
	students = list(map(int, input().split()))
	print(sequence(n, students))


q = int(input())
for query in range(q):
	n = int(input())
	perm = list(map(int, input().split()))
	x = int(perm.index(1))
	test = []
	i = x
	while i - x < n:
		test.append(perm[i % n])
		i += 1
	test2 = []
	j = x
	while x - j < n:
		test2.append(perm[j % n])
		j -= 1
	if test == sorted(test) or test2 == sorted(test2):
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	c1 = -999
	c = -999
	n = int(input())
	flag = 0
	a = [int(x) for x in input().split()]
	if n == 1:
		print('YES')
		continue
	if a[0] == n:
		if a[1] == 1:
			c = -1
		else:
			c = 1
	elif a[0] == 1:
		if a[1] == n:
			c = 1
		else:
			c = -1
	if c == -999:
		c = a[0] - a[1]
	if a[0] == 1:
		if a[n - 1] == 2:
			c1 = 1
		if a[n - 1] == n:
			c1 = -1
	elif a[0] == n:
		if a[n - 1] == n - 1:
			c1 = -1
		if a[n - 1] == 1:
			c1 = 1
	if c1 == -999:
		c1 = a[n - 1] - a[0]
	for i in range(2, n):
		if c1 != c:
			flag = 1
			break
		if a[i] == n and a[i - 1] == 1:
			c1 = 1
			if c != c1:
				flag = 1
				break
		elif a[i] == n and a[i - 1] == n - 1:
			c1 = -1
			if c != c1:
				flag = 1
				break
		elif a[i] == 1 and a[i - 1] == n:
			c1 = -1
			if c != c1:
				flag = 1
				break
		elif a[i] == 1 and a[i - 1] == 2:
			c1 = 1
			if c != c1:
				flag = 1
				break
		else:
			c1 = a[i - 1] - a[i]
			if c != c1:
				flag = 1
				break
	if flag == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
for q in range(0, t):
	n = int(input())
	a = [int(i) for i in input().split()]
	ma = max(a)
	mi = min(a)
	mai = 0
	mii = 0
	for i in range(0, n):
		if ma == a[i]:
			mai = i
		elif mi == a[i]:
			mii = i
	flag = 0
	i = mai
	count = 0
	l = max(a)
	while count != n:
		if a[i] != l:
			flag = 1
			break
		l -= 1
		count += 1
		i += 1
		if i == n:
			i = 0
	flag1 = 0
	i = mii
	count = 0
	l = min(a)
	while count != n:
		if a[i] != l:
			flag1 = 1
			break
		l += 1
		count += 1
		i += 1
		if i == n:
			i = 0
	if flag == 0 or flag1 == 0:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	t -= 1
	N = int(input())
	arr = list(map(int, input().split()))
	two = arr + arr
	forward = sorted(arr)
	reverse = forward[::-1]
	can = False
	for i in range(N + N):
		num = two[i]
		if two[i] == forward[0]:
			temp = i
			count = 0
			found = True
			matching = 1
			while i + N - 1 < N + N and count + 1 < N and (temp + 1 < N + N):
				temp += 1
				count += 1
				if two[temp] != forward[count]:
					found = False
					break
				else:
					matching += 1
			if found and matching == N:
				can = True
				break
		elif two[i] == reverse[0]:
			temp = i
			count = 0
			found = True
			matching = 1
			while i + N - 1 < N + N and count + 1 < N and (temp + 1 < N + N):
				temp += 1
				count += 1
				if two[temp] != reverse[count]:
					found = False
					break
				else:
					matching += 1
			if found and matching == N:
				can = True
				break
	if can:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	p = list(map(int, input().split(' ')))
	index = p.index(1)
	a = p
	c = 0
	if p[p.index(1) - 1] == 2:
		a = list(reversed(p))
	for j in range(n):
		if index + 1 > n - 1:
			index = 0
		else:
			index += 1
		if a[index] - a[index - 1] != 1 and a[index] != 1:
			print('NO')
			break
		else:
			c += 1
	if c == n:
		print('YES')


t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	A *= 2
	B = A.copy()
	CH1 = [i for i in range(1, n + 1)]
	CH2 = CH1[::-1]
	ind1 = A.index(1)
	A = A[ind1:ind1 + n]
	ind2 = B.index(n)
	B = B[ind2:ind2 + n]
	if A == CH1 or B == CH2:
		print('YES')
	else:
		print('NO')


q = int(input())
for query in range(q):
	n = int(input())
	p = [int(x) for x in input().split(' ')]
	i = p.index(1)
	if p[i:] + p[:i] == [j for j in range(1, n + 1)]:
		ans = 'YES'
	elif p[:i + 1][::-1] + p[n - 1:i:-1] == [j for j in range(1, n + 1)]:
		ans = 'YES'
	else:
		ans = 'NO'
	print(ans)


q = int(input())

def hor(n, p):
	i = 1
	while i < n:
		if p[i] != (p[i - 1] + 1) % n:
			break
		i += 1
	if i == n:
		return 'YES'
	if i > 1:
		return 'NO'
	for i in range(1, n):
		if p[i - 1] != (p[i] + 1) % n:
			return 'NO'
	return 'YES'
for _ in range(q):
	n = int(input())
	p = list(map(lambda x: int(x) - 1, input().split()))
	print(hor(n, p))


def solve(n, p):
	for i in range(n):
		pp = p[i:] + p[:i]
		if pp == list(range(1, n + 1)) or pp == list(range(n, 0, -1)):
			print('YES')
			return
	print('NO')
q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(x) for x in input().split()]
	solve(n, p)


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split(' ')))
	pp = [x for x in range(1, n + 1)]
	i = p.index(n)
	a = p[i + 1:] + p[:i + 1]
	flag1 = True
	for (x, y) in zip(pp, a):
		if x != y:
			flag1 = False
	p = p[::-1]
	i = p.index(n)
	b = p[i + 1:] + p[:i + 1]
	flag2 = True
	for (x, y) in zip(pp, b):
		if x != y:
			flag2 = False
	if flag1 or flag2:
		print('YES')
	else:
		print('NO')


query = int(input())
for i in range(query):
	human_number = int(input())
	list = input().split()
	count = 0
	stick = human_number - 1
	for j in range(stick):
		if int(list[j]) - int(list[j + 1]) == 1 or int(list[j + 1]) - int(list[j]) == 1:
			pass
		elif int(list[j]) - int(list[j + 1]) == stick or int(list[j + 1]) - int(list[j]) == stick:
			pass
		else:
			count += 1
	if count == 0:
		print('YES')
	else:
		print('NO')


def nextInt():
	return int(input())

def nextInts():
	return list(map(int, input().split()))

def ok(p):
	n = len(p)
	for i in range(n):
		x = p[i] % n + 1
		if p[(i + 1) % n] != x:
			return False
	return True

def solve():
	n = nextInt()
	p = nextInts()
	if ok(p):
		return True
	p.reverse()
	return ok(p)
q = nextInt()
for i in range(q):
	print('YES' if solve() else 'NO')


num_test = int(input())

def clock(nums):
	arg_min = 0
	for i in range(len(nums)):
		if nums[i] == 1:
			arg_min = i
	i = arg_min
	while i % len(nums) != (arg_min - 1) % len(nums):
		if nums[(i + 1) % len(nums)] - nums[i % len(nums)] != 1:
			return False
		i += 1
	return True

def anti(nums):
	arg_max = 0
	for i in range(len(nums)):
		if nums[i] == len(nums):
			arg_max = i
	i = arg_max
	while i % len(nums) != (arg_max - 1) % len(nums):
		if nums[i % len(nums)] - nums[(i + 1) % len(nums)] != 1:
			return False
		i += 1
	return True
for i in range(num_test):
	num_stud = int(input())
	students = list(map(int, input().split()))
	if clock(students) or anti(students):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1 or n == 2:
		print('YES')
	else:
		for i in range(n - 1):
			if a[i] == 1 and a[i + 1] == n or (a[i] == n and a[i + 1] == 1):
				continue
			elif abs(a[i] - a[i + 1]) != 1:
				print('NO')
				break
		else:
			print('YES')


import io, os
q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	f = True
	if 1:
		for i in range(-1, n - 1):
			if a[i] % n + 1 != a[i + 1]:
				f = False
				break
		if f:
			print('YES')
	if f == False:
		f = True
		for i in range(-1, n - 1):
			if a[i] != a[i + 1] % n + 1:
				print('NO')
				f = False
				break
		if f:
			print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	lis = list(map(int, input().split()))
	b = lis.index(1)
	aux = True
	if n > 2:
		for i in range(b):
			el = lis[0]
			lis.remove(el)
			lis.append(el)
		if lis[1] != 2:
			lis.remove(1)
			lis.append(1)
		if lis[1] > lis[0]:
			for i in range(1, n):
				if lis[i - 1] != lis[i] - 1:
					aux = False
					break
		else:
			for i in range(1, n):
				if lis[i - 1] != lis[i] + 1:
					aux = False
					break
	if aux:
		print('YES')
	else:
		print('NO')


a = int(input())
for i in range(a):
	b = int(input())
	A = list(map(int, input().split()))
	q = A.index(1)
	j = 0
	t = True
	v = 1
	if A[q - 1] == 2:
		while j < b - 1 and t == True:
			if A[q - 1] == v + 1:
				v += 1
				q -= 1
				j += 1
			else:
				t = False
		if t == True:
			print('YES')
		else:
			print('NO')
	elif A[q - 1] == b:
		v = b
		q -= 1
		j += 1
		while j < b and t == True:
			if A[q - 1] == v - 1:
				v -= 1
				q -= 1
				j += 1
			else:
				t = False
		if t == True:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


def main():
	for _ in range(int(input())):
		n = int(input())
		l = list(map(int, input().split()))
		c = 0
		if n == 1:
			print('YES')
			continue
		for i in range(n - 1):
			if l[i] < l[i + 1]:
				c += 1
		if l[n - 1] < l[0]:
			c += 1
		if c == n - 1 or c == 1:
			print('YES')
		else:
			print('NO')
	return
main()


q = int(input())
for _ in range(q):
	n = int(input())
	stu = list(map(int, input().split()))
	k = stu[0]
	if stu == list(range(k, n + 1)) + list(range(1, k)) or stu == list(range(k, 0, -1)) + list(range(n, k, -1)):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	p = tuple(map(int, input().split()))
	for q in (p, p[::-1]):
		if len({(a + n - b) % n for (a, b) in enumerate(q)}) == 1:
			print('YES')
			break
	else:
		print('NO')


loop = int(input())
for i in range(loop):
	n = int(input())
	count1 = 0
	count2 = 0
	arr = list(map(int, input().split()))[:n]
	c = len(arr)
	for i in range(c):
		if arr[i] > arr[i - 1]:
			count1 += 1
		else:
			count2 += 1
	if count1 < 2 or count2 < 2:
		print('YES')
	else:
		print('NO')


T = int(input())
for t in range(T):
	d = True
	d1 = True
	num = int(input())
	c = True
	c1 = True
	my_list = list(map(int, input().split()))
	for i in range(num - 1):
		if my_list[i + 1] != my_list[i] - 1:
			if my_list[i] == 1 and d:
				d = False
			else:
				d1 = False
		if my_list[i + 1] != my_list[i] + 1:
			if my_list[i + 1] == 1 and c:
				c = False
			else:
				c1 = False
	if c1 or d1:
		print('YES')
	else:
		print('NO')


from collections import deque, Counter
import array
from itertools import combinations, permutations
from math import sqrt
import unittest

def read_int():
	return int(input().strip())

def read_int_array():
	return [int(i) for i in input().strip().split(' ')]
for _ in range(read_int()):
	n = read_int()
	p = read_int_array()
	if n == 1:
		print('YES')
		continue
	step = p[1] - p[0]
	if abs(step) != 1 and n > 2:
		step = p[2] - p[1]
	if abs(step) != 1:
		print('NO')
		continue
	breaks = 0
	for i in range(n):
		if p[i % n] + step != p[(i + 1) % n]:
			breaks += 1
	print('YES' if breaks == 1 else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	flag = False
	a = list(map(int, input().split()))
	b = sorted(a)
	d = sorted(a, reverse=True)
	x = 0
	y = 0
	for i in range(n):
		c = a[i:] + a[:i]
		if c == b or c == d:
			flag = True
	if flag == True:
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = [int(x) for x in input().split()]
	f = 'NO'
	if n > 1:
		k = a[0] - a[n - 1]
		if abs(k) == 1 or abs(k) == n - 1:
			if k > 0 and abs(k) != n - 1 or k == 1 - n:
				k = 1
			else:
				k = -1
			f = 'YES'
			for j in range(1, n):
				if a[j] - a[j - 1] != k and abs(a[j] - a[j - 1]) != n - 1:
					f = 'NO'
					break
	else:
		f = 'YES'
	print(f)


q = int(input())

def is_sublist(a, b):
	if not a:
		return True
	if not b:
		return False
	return b[:len(a)] == a or is_sublist(a, b[1:])
for _ in range(0, q):
	n = int(input())
	P = list(map(int, input().split()))
	CW = P + P
	CCW = P[::-1] + P[::-1]
	R = list(range(1, n + 1))
	if is_sublist(R, CW):
		print('YES')
	elif is_sublist(R, CCW):
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	cnt = 0
	for i in range(1, n):
		if abs(a[i] - a[i - 1]) > 1:
			cnt += 1
	if abs(a[0] - a[-1]) > 1:
		cnt += 1
	if cnt > 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	flag = True
	for i in range(n - 1):
		if a[i] - a[i + 1] != 1 and a[i + 1] - a[i] != 1 and (a[i] - a[i + 1] != n - 1) and (a[i + 1] - a[i] != n - 1):
			flag = False
	print('YES' if flag else 'NO')


def dada():
	n = int(input())
	a = list(map(int, input().split()))
	nsp = a
	for i in range(1, len(a)):
		if abs(a[i] - a[i - 1]) != 1:
			nsp = a[i:] + a[0:i]
			break
	if nsp == list(range(1, n + 1)) or nsp == list(range(1, n + 1))[::-1]:
		print('YES')
	else:
		print('NO')
al = int(input())
for u in range(al):
	dada()


def is_circular_sorted(arr):
	count = 0
	countr = 0
	length = len(arr)
	for i in range(length):
		if arr[i] > arr[(i + 1) % len(arr)]:
			count += 1
	for i in range(length):
		if arr[i] < arr[(i + 1) % len(arr)]:
			countr += 1
	return count <= 1 or countr <= 1
n = int(input())
for i in range(n):
	x = int(input())
	l = list(map(int, input().split()))
	if is_circular_sorted(l) == True:
		print('YES')
	else:
		print('NO')


a = int(input())
for i in range(a):
	s = int(input())
	z = list(map(int, input().split()))
	z = z * 2
	x = []
	y = []
	for i in range(len(z)):
		if z[i] == 1:
			x.append(i)
		if z[i] == s:
			y.append(i)
	t = z[x[0]:x[0] + s]
	r = z[y[0]:y[0] + s]
	flag = 0
	k = sorted(t)
	m = sorted(r, reverse=True)
	if t == k or r == m:
		print('YES')
	else:
		print('NO')


N = int(input())
for j in range(N):
	pi = int(input())
	line = input()
	line = line + ' ' + line
	to_comp_1 = ''
	to_comp_2 = ''
	for i in range(1, pi + 1):
		to_comp_1 = to_comp_1 + str(i)
		to_comp_2 = to_comp_2 + str(pi - i + 1)
		if i != pi:
			to_comp_1 += ' '
			to_comp_2 += ' '
	if to_comp_1 in line or to_comp_2 in line:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	nn = n
	p = list(map(int, input().split()))
	ind = p.index(1)
	c = i = 0
	while i < n:
		if abs(p[i] - p[i - 1]) > 1:
			c += 1
		i += 1
	print('YES' if c < 2 else 'NO')


import sys
rounds = int(sys.stdin.readline())
for x in range(rounds):
	n = int(sys.stdin.readline())
	nums = [int(x) for x in sys.stdin.readline().split()]
	result = True
	if n != 1:
		last = nums[0]
		dir = nums[1] == nums[0] + 1 or (nums[0] == n and nums[1] == 1)
		for num in nums[1:]:
			if num != last + (1 if dir else -1) and (not (last == n and num == 1 if dir else last == 1 and num == n)):
				result = False
				break
			last = num
	print('YES' if result else 'NO')


t = int(input())
for _ in range(0, t):
	n = int(input())
	l = list(map(int, input().split()))
	flag = 0
	if n == 1:
		print('YES')
		continue
	gt = l[0] < l[1]
	c = 0
	g = l[0]
	c1 = 0
	c2 = 0
	for i in range(0, n):
		if l[0 - i] > l[0 - i - 1]:
			c1 = c1 + 1
		if c1 > 1:
			break
	for i in range(0, n):
		if l[0 - i] < l[0 - i - 1]:
			c2 = c2 + 1
		if c2 > 1:
			break
	if c1 > 1 and c2 > 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	flag2 = 0
	for i in range(n):
		if a[i] != a[(i + 1) % n] + 1 and a[i] != 1 and (a[i] != n):
			flag = 1
	for i in range(n):
		if a[i] != a[(i + 1) % n] - 1 and a[i] != 1 and (a[i] != n):
			flag2 = 1
	if flag == 0 or flag2 == 0:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	f = 0
	l = list(map(int, input().split()))
	l1 = l.copy()
	l1.sort()
	l4 = l1.copy()
	l4.reverse()
	a = l.index(1)
	l2 = []
	l2.append(l[a])
	l3 = []
	l3.append(l[a])
	for i in range(a + 1, n):
		l3.append(l[i])
	for i in range(a):
		l3.append(l[i])
	for i in range(a - 1, -1, -1):
		l2.append(l[i])
	for i in range(n - 1, a, -1):
		l2.append(l[i])
	if l3 == l4 or l2 == l4 or l3 == l1 or (l2 == l1):
		print('YES')
	else:
		print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	a = list(map(int, input().split()))
	flag = 0
	for i in range(n - 1):
		if a[i] == n:
			if a[i + 1] != 1:
				flag = -1
				break
		elif a[i] != n:
			if a[i + 1] != a[i] + 1:
				flag = -1
				break
	if flag == 0:
		print('Yes')
	else:
		flag = 0
		for i in range(1, n):
			if a[i] == n:
				if a[i - 1] != 1:
					flag = -1
					break
			elif a[i] != n:
				if a[i - 1] != a[i] + 1:
					flag = -1
					break
		if flag == 0:
			print('Yes')
		else:
			print('No')


numberOfQueries = int(input())
for _ in range(numberOfQueries):
	numberOfStudents = int(input())
	students = list(map(int, input().split()))
	canDoRoundelay = True
	for i in range(numberOfStudents):
		if students[i] == 1:
			index = i
			for _ in range(numberOfStudents - 1):
				if students[index % numberOfStudents] != students[(index + 1) % numberOfStudents] - 1:
					canDoRoundelay = False
					break
				index += 1
	if canDoRoundelay:
		print('YES')
		continue
	canDoRoundelay = True
	for i in range(numberOfStudents):
		if students[i] == numberOfStudents:
			index = i
			for _ in range(numberOfStudents - 1):
				if students[index % numberOfStudents] - 1 != students[(index + 1) % numberOfStudents]:
					canDoRoundelay = False
					break
				index += 1
	print('YES' if canDoRoundelay else 'NO')


import math
from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
li = lambda : list(map(int, input().split()))
m = int(input())
for j in range(0, m):
	n = int(input())
	a = li()
	m = min(a)
	k = 0
	f = g = 0
	for i in range(0, len(a)):
		if a[i] == m:
			k = i
			break
	a = a[k:] + a[0:k]
	for i in range(0, n - 1):
		if a[i] > a[i + 1]:
			f = 1
	for i in range(1, n - 1):
		if a[i] < a[i + 1]:
			g = 1
	if f == 1 and g == 1:
		print('NO')
	else:
		print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	prev = p[0]
	for s in p[1:]:
		if abs(prev - s) not in [1, n - 1]:
			print('NO')
			break
		prev = s
	else:
		print('YES')


q = int(input())
for _ in range(q):
	n = int(input())
	st = list(map(int, input().split()))
	mn = 0
	for s in range(n):
		if st[s] < st[mn]:
			mn = s
	st1 = []
	for i in range(mn, n):
		st1.append(st[i])
	for i in range(mn):
		st1.append(st[i])
	st2 = []
	for i in range(mn + 1, n):
		st2.append(st[i])
	for i in range(mn + 1):
		st2.append(st[i])
	if st1 == sorted(st) or st2 == sorted(st, reverse=True):
		print('YES')
	else:
		print('NO')


def checkequal(a, b):
	flag = 0
	for i in range(0, len(a)):
		if a[i] == b[i]:
			flag = 1
		else:
			flag = 0
			break
	if flag == 1:
		return 1
	else:
		return 0

def checkequalrev(a, b):
	flag = 0
	for i in range(0, len(a)):
		if a[i] == b[i]:
			flag = 1
		else:
			flag = 0
			break
	if flag == 1:
		return 1
	else:
		return 0
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	checkrev = arr.copy()
	brr = arr.copy()
	checkrev.sort(reverse=True)
	brr.sort()
	getans = checkequal(arr, brr)
	getans1 = checkequalrev(arr, checkrev)
	if getans == 1 or getans1 == 1:
		print('YES')
	else:
		flag = 0
		for i in range(n):
			arr = arr[-1:] + arr[:-1]
			getans1 = checkequalrev(arr, checkrev)
			getans = checkequal(arr, brr)
			if getans1 == 1 or getans == 1:
				flag = 1
				break
			else:
				flag = 0
		if flag == 1:
			print('YES')
		else:
			print('NO')


def can_cwd(p):
	n = len(p)
	for i in range(1, n):
		if p[i] != 1:
			if p[i] != p[i - 1] + 1:
				return False
	return True

def can_ccwd(p):
	n = len(p)
	for i in range(1, n):
		if p[i] != n:
			if p[i] != p[i - 1] - 1:
				return False
	return True
q = int(input())
for _ in range(q):
	n = int(input())
	p = [int(i) for i in input().split()]
	print('YES' if can_cwd(p) or can_ccwd(p) else 'NO')


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	dic = {}
	for i in range(n):
		dic[l[i]] = i
	flag = 0
	for i in range(1, n):
		if abs(dic[i] - dic[i + 1]) != 1:
			if dic[i] == 0 and dic[i + 1] == n - 1:
				continue
			elif dic[i] == n - 1 and dic[i + 1] == 0:
				continue
			else:
				flag = 1
				break
	if flag == 1:
		print('NO')
	else:
		print('YES')


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
ans = []
for _ in range(int(input())):
	n = int(z())
	lst = zzz()
	l = r = lst.index(1)
	c = 0
	d = 0
	j = 2
	for _ in range(n - 1):
		l += 1
		r -= 1
		if l >= n:
			l = 0
		if r < 0:
			r = n - 1
		p = lst.index(j)
		if p != l:
			c = 1
		if p != r:
			d = 1
		j += 1
	if c + d == 2:
		print('NO')
	else:
		print('YES')
for i in ans:
	print(i)


import sys
import math
import collections
import bisect
import string

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(int(input())):
	n = int(input())
	arr = get_list()
	no = 1
	start = arr.index(no)
	poss = True
	while no <= n:
		ele = arr[start % n]
		if ele == no:
			no += 1
			start += 1
			continue
		else:
			poss = False
			break
	if poss:
		print('YES')
		continue
	no = n
	start = arr.index(no)
	poss = True
	while no > 1:
		ele = arr[start % n]
		if ele == no:
			no -= 1
			start += 1
			continue
		else:
			poss = False
			break
	if poss:
		print('YES')
		continue
	print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	p = list(map(int, input().split()))
	flag = True
	a = p[0]
	if n != 1:
		for i in range(1, n):
			b = (p[i - 1] + 1) % n
			if b == 0:
				b = n
			if p[i] == b:
				continue
			else:
				flag = False
				break
		if not flag:
			flag = True
			for i in range(1, n):
				b = (p[i - 1] - 1 + n) % n
				if b == 0:
					b = n
				if p[i] == b:
					continue
				else:
					flag = False
					break
	if flag:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	inc = []
	dec = []
	for i in range(1, len(arr)):
		if arr[i] > arr[i - 1]:
			inc += [arr[i] - arr[i - 1]]
		else:
			dec += [abs(arr[i] - arr[i - 1])]
	if len(inc) == 0 or len(dec) == 0:
		print('YES')
	elif len(inc) == 1 and inc[0] == n - 1 and (sum(dec) == len(dec)):
		print('YES')
	elif len(dec) == 1 and dec[0] == n - 1 and (sum(inc) == len(inc)):
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	s = [int(i) for i in input().split()]
	if n <= 2:
		print('YES')
		continue
	beg = s.index(1)
	fl = True
	if s[(beg + 1) % n] == 2:
		for i in range(beg + 1, beg + n):
			if s[i % n] != i - beg + 1:
				fl = False
				break
	elif s[(beg + 1) % n] == n:
		for i in range(beg + 1, beg + n):
			if s[i % n] != n - (i - beg) + 1:
				fl = False
				break
	else:
		fl = False
	print('YES' if fl else 'NO')


import sys

def main():
	q = int(sys.stdin.readline().rstrip())
	for i in range(q):
		n = int(sys.stdin.readline().rstrip())
		arr = [int(x) for x in sys.stdin.readline().split(' ')]
		if forward(n, arr) or forward(n, arr[::-1]):
			print('YES')
		else:
			print('NO')

def forward(n, arr):
	if n <= 1:
		return True
	for i in range(1, n):
		if arr[i] == 1 and arr[i - 1] == n:
			continue
		elif arr[i] == arr[i - 1] + 1:
			continue
		else:
			break
	else:
		return True
	return False
main()


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	begin = end = -1
	for i in range(len(a)):
		if a[i] == 1:
			begin = i
		if a[i] == n:
			end = i
		if begin != -1 and end != -1:
			break
	ans = 'YES'
	last = 0
	for elem in a[begin:] + a[:begin]:
		if elem == last + 1:
			last = elem
		else:
			last = n + 1
			for elem in a[end:] + a[:end]:
				if elem == last - 1:
					last = elem
				else:
					ans = 'NO'
					break
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	ed = a.index(1)
	shag = 0
	flag = True
	if n == 1:
		print('YES')
		continue
	if ed == n - 1 and a[0] == 2:
		shag = 1
	elif a[(ed + 1) % n] == 2:
		shag = 1
	elif ed == 0 and a[-1] == 2:
		shag = -1
	elif a[ed - 1] == 2:
		shag = -1
	if shag == 0:
		print('NO')
		continue
	elif shag == 1:
		for k in range(1, n):
			if a[(ed + k) % n] - a[(ed + k - 1) % n] != 1:
				print('NO')
				flag = False
				break
	else:
		for k in range(1, n):
			if a[(ed - k) % n] - a[(ed - k + 1) % n] != 1:
				print('NO')
				flag = False
				break
	if flag:
		print('YES')


from math import *
from collections import *
from itertools import *
from functools import *
from random import *

def getl(t=int):
	return [t(x) for x in input().split()]

def get(t=int):
	return t(input())
alphabet = [chr(x) for x in range(ord('a'), ord('z') + 1)]
alnum = lambda x: ord(x) - ord('a')

def main():
	for i in range(get()):
		print(test())

def test():
	n = get()
	a = getl()
	if all(((a[i] - a[(i + 1) % n] + n) % n == 1 or (a[i] - a[(i + 1) % n] + n) % n == n - 1 for i in range(n))):
		return 'YES'
	else:
		return 'NO'
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(clock, anti, i) = (0, 0, 0)
	for i in range(1, n + 1):
		if a[i - 1] == n:
			if a[i % n] == 1:
				clock += 1
		elif a[i - 1] + 1 == a[i % n]:
			clock += 1
		if a[i - 1] == 1:
			if a[i % n] == n:
				anti += 1
		elif a[i - 1] - 1 == a[i % n]:
			anti += 1
	if clock == n or anti == n:
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	n = int(input())
	s = input().split(' ')
	if n == 0:
		print('YES')
	else:
		flag = 0
		for j in range(n - 1):
			if int(s[j]) == n and int(s[j + 1]) == 1 or (int(s[j]) == n and int(s[j + 1]) == n - 1) or (int(s[j]) == 1 and int(s[j + 1]) == 2) or (int(s[j]) == 1 and int(s[j + 1]) == n):
				continue
			elif int(s[j]) == int(s[j + 1]) - 1 or int(s[j]) == int(s[j + 1]) + 1:
				continue
			else:
				flag = 1
				print('NO')
				break
		if flag == 0:
			print('YES')


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	maxint = 10 ** 10
	max_so_far = -maxint - 1
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def solve():
	n = int(input())
	arr = list_input()
	if n == 1:
		if arr[0] == 1:
			print('YES')
		else:
			print('NO')
		return
	start = indx = arr.index(1)
	if arr[(indx + 1) % n] == 2:
		val = 2
		indx = (indx + 1) % n
		while indx != start:
			if arr[indx] != val:
				print('NO')
				return
			indx = (indx + 1) % n
			val += 1
		print('YES')
	elif arr[indx - 1] == 2:
		val = 2
		indx -= 1
		while indx != start:
			if indx == -1:
				indx = n - 1
				if indx == start:
					break
			if arr[indx] != val:
				print('NO')
				return
			val += 1
			indx -= 1
		print('YES')
	else:
		print('NO')
t = 1
t = int(input())
for _ in range(t):
	solve()


q = int(input())

def task_A(n, l):
	b_incr = True
	b_decr = True
	gap_incr = 0
	gap_decr = 0
	for i in range(n - 1):
		if l[i] - l[i + 1] != -1:
			gap_incr = i + 1
			break
	for i in range(gap_incr, n - 1):
		if l[i] - l[i + 1] != -1:
			b_incr = False
			break
	for i in range(n - 1):
		if l[i] - l[i + 1] != 1:
			gap_decr = i + 1
			break
	for i in range(gap_decr, n - 1):
		if l[i] - l[i + 1] != 1:
			b_decr = False
			break
	if b_incr or b_decr:
		return 'YES'
	else:
		return 'NO'
for i in range(q):
	n = int(input())
	l = list(map(int, input().split(' ')))
	print(task_A(n, l))


import os, sys
q = int(input())
for i in range(q):
	n = int(input().strip())
	text = sys.stdin.readline()
	p = list(map(int, text.strip().split()))
	diffs = []
	for (e, z) in zip(p[:-1], p[1:]):
		diffs.append(z - e)
	diffs.sort()
	if diffs.count(1) >= len(diffs) - 1:
		print('YES')
	elif diffs.count(-1) >= len(diffs) - 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for qq in range(q):
	n = int(input())
	dat = list(map(int, input().split()))
	can1 = True
	can2 = True
	cur = dat[0]
	for i in range(n):
		if dat[i] == 1:
			p1 = i
			break
	lorig = list(range(1, n + 1))
	t = n - p1
	col = lorig[t:] + lorig[:t]
	for i in range(n):
		if dat[i] != col[i]:
			can1 = False
	for i in range(n):
		if dat[i] == n:
			p1 = i
			break
	lorig = list(range(n, 0, -1))
	t = n - p1
	col = lorig[t:] + lorig[:t]
	for i in range(n):
		if dat[i] != col[i]:
			can2 = False
	print('YES' if can1 or can2 else 'NO')


T = int(input())
t = 0
while t < T:
	n = int(input())
	ok = True
	p = [int(i) for i in input().split()]
	ter = p.index(1)
	cur = (ter + 1) % n
	tmp = p[cur]
	d = None
	while cur != ter:
		if d == None:
			if p[cur] == n:
				d = -1
				tmp = n
			elif p[cur] == 2:
				d = 1
				tmp = 2
			else:
				ok = False
				break
			cur = (cur + 1) % n
			continue
		if p[cur] != tmp + d:
			ok = False
			break
		cur = (cur + 1) % n
		tmp += d
	if ok:
		print('YES')
	else:
		print('NO')
	t += 1


def check(nums, p, upd):
	number = 1
	while number <= len(nums):
		if nums[p] == number:
			p += upd
			if p == -1:
				p = len(nums) - 1
			if p == len(nums):
				p = 0
			number += 1
		else:
			return False
	return True
for _ in range(int(input())):
	n = int(input())
	nums = [int(j) for j in input().split()]
	p = nums.index(1)
	if check(nums, p, 1) == True or check(nums, p, -1):
		print('YES')
	else:
		print('NO')


def answer():
	np = int(input())
	nums = list(map(int, input().split(' ')))
	s_val = nums[0]
	order0 = list(range(1, np + 1))
	si = order0.index(s_val)
	order = order0[si:] + order0[:si]
	if order == nums:
		return 'YES'
	rev_order0 = list(range(np, 0, -1))
	si = rev_order0.index(s_val)
	rev_order = rev_order0[si:] + rev_order0[:si]
	if rev_order == nums:
		return 'YES'
	else:
		return 'NO'
n = int(input())
for i in range(n):
	out = answer()
	print(out)


T = int(input())
for _ in range(T):
	n = int(input())
	arr = list(map(int, input().rstrip().split()))
	a = arr.index(1)
	clo = True
	coun = True
	for j in range(1, n):
		clo &= arr[(a - j + n) % n] == j + 1
		coun &= arr[(a + j + n) % n] == j + 1
	if clo | coun:
		print('YES')
	else:
		print('NO')


from collections import Counter
t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	c = Counter(((u - v) % n for (u, v) in zip(x, x[1:])))
	print('YES' if c[1] == n - 1 or c[n - 1] == n - 1 else 'NO')


for case in range(int(input())):
	n = int(input())
	a = input().split()
	fl = True
	for i in range(n):
		a[i] = int(a[i])
	for i in range(n - 1):
		if a[i] - a[i + 1] != 1 and a[i] - a[i + 1] != -1 and (a[i] - a[i + 1] != n - 1) and (a[i] - a[i + 1] != 1 - n):
			fl = False
	print('Yes' if fl else 'No')


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	clk = [i for i in range(1, n + 1)]
	aclk = [i for i in range(n, 1, -1)]
	aclk.insert(0, 1)
	if n in [1, 2]:
		print('YES')
	else:
		while l.index(1) != 0:
			temp = l.pop(0)
			l.append(temp)
		if l == clk or l == aclk:
			print('YES')
		else:
			print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	cou = 0
	for j in range(1, n):
		if abs(l[j] - l[j - 1]) != 1:
			if abs(l[j] - l[j - 1]) == n - 1:
				cou += 1
			else:
				cou = 3
				break
	if cou <= 1:
		print('YES')
	else:
		print('NO')


q = int(input())
for _ in range(q):
	n = int(input())
	a = [int(s) for s in input().split()]
	forw = back = 1
	for i in range(n):
		if a[i - 1] - a[i] == 1 or (a[i] == n and a[i - 1] == 1) or (a[i - 1] == n and a[i] == 1):
			pass
		else:
			back = 0
		if a[(i + 1) % n] - a[i] == 1 or (a[(i + 1) % n] == n and a[i] == 1) or (a[(i + 1) % n] == 1 and a[i] == n):
			pass
		else:
			forw = 0
	if forw or back:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	k = 0
	if n > 1:
		z = a[1] - a[0]
		if z == 1 or z == -(n - 1):
			k = 1
		else:
			k = 2
		for j in range(1, n - 1):
			m = a[j + 1] - a[j]
			if (m == 1 or m == -(n - 1)) and k == 1:
				k = 1
			elif (m == -1 or m == n - 1) and k == 2:
				k = 2
			else:
				z = 22
				break
		if z != 22:
			print('YES')
		else:
			print('NO')
	else:
		print('YES')


q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	p = True
	for i in range(n - 1):
		if abs(l[i] - l[i + 1]) == 1 or abs(l[i] - l[i + 1]) == n - 1:
			p = p and True
		else:
			p = p and False
	if p:
		oneind = l.index(1)
		leftone = l[:oneind]
		rightone = l[oneind + 1:]
		if leftone == sorted(leftone) and rightone == sorted(rightone):
			print('YES')
		elif leftone == sorted(leftone, reverse=True) and rightone == sorted(rightone, reverse=True):
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = [int(i) for i in input().split()]
	clock = True
	ans_clock = True
	counter = True
	ans_counter = True
	for i in range(n - 1):
		if l[i + 1] == l[i] + 1:
			pass
		elif l[i + 1] == 1 and clock:
			clock = False
		else:
			ans_clock = False
		if l[i + 1] == l[i] - 1:
			pass
		elif l[i] == 1 and counter:
			counter = False
		else:
			ans_counter = False
	if ans_clock or ans_counter:
		print('YES')
	else:
		print('NO')


import sys

def clockwise(students, n):
	start = students.index(1)
	for i in range(n - 1):
		if students[(start + i) % n] + 1 != students[(start + i + 1) % n]:
			return False
	return True

def counterClockwise(students, n):
	start = students.index(1)
	for i in range(n - 1):
		if students[(start - i) % n] + 1 != students[(start - i - 1) % n]:
			return False
	return True
tests = int(input())
for i in range(tests):
	n = int(input())
	students = [int(x) for x in sys.stdin.readline().split()]
	if clockwise(students, n) or counterClockwise(students, n):
		print('YES\n')
	else:
		print('NO\n')


def int_multiple():
	return [int(c) for c in input().split()]

def int_single():
	return int(input())

def str_multiple():
	return [c for c in input().split()]

def str_single():
	return input()
q = int_single()
res = []
for i in range(q):
	n = int_single()
	l = int_multiple()
	ctclk = True
	clk = True
	fst = l.index(1)
	tmp = l[fst:] + l[:fst]
	tmp1 = []
	if fst != n - 1:
		tmp1 = l[fst + 1:] + l[:fst + 1]
	else:
		tmp1 = l
	for j in range(n - 1):
		if tmp[j] >= tmp[j + 1]:
			clk = False
		if tmp1[j] <= tmp1[j + 1]:
			ctclk = False
	if clk or ctclk:
		res.append('YES')
	else:
		res.append('NO')
for r in res:
	print(r)


q = int(input())
for j in range(q):
	q1 = int(input())
	n = list(map(int, input().split()))
	if len(n) == 1:
		print('YES')
	else:
		i = 0
		for s in range(len(n)):
			if n[s] == n[s - 1] - 1:
				i += 1
			elif n[s] == n[s - 1] + 1:
				i += 1
		if i >= len(n) - 1:
			print('YES')
		else:
			print('NO')


q = int(input())
v = []
for i in range(q):
	n = int(input())
	v.append(list(map(int, input().split())))
for i in v:
	poss = True
	if len(i) <= 2:
		print('YES')
	else:
		up = None
		for j in range(-1, len(i) - 1):
			if i[j] + 1 == i[j + 1] or (i[j] == len(i) and i[j + 1] == 1):
				if up == None:
					up = 1
				elif up == 0:
					poss = False
					break
			elif i[j] == i[j + 1] + 1 or (i[j] == 1 and i[j + 1] == len(i)):
				if up == None:
					up = 0
				elif up == 1:
					poss = False
					break
			else:
				poss = False
				break
		if poss:
			print('YES')
		else:
			print('NO')


q = int(input())
for i in range(q):
	n = int(input())
	l = list(map(int, input().split()))
	if n <= 2:
		print('YES')
		continue
	first = l.index(1)
	p = first
	direction = 1
	if l[(first + direction) % n] == 2:
		pass
	elif l[(first - direction) % n] == 2:
		direction = -1
	else:
		print('NO')
		continue
	for j in range(n - 1):
		if l[(p + direction) % n] == l[p] + 1:
			p = (p + direction) % n
		else:
			print('NO')
			break
	if p == (first - direction) % n:
		print('YES')


for i in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().strip().split(' ')]
	b = a.index(max(a))
	c = a[b:] + a[:b]
	if c[0] - c[-1] == 1:
		e = c[1:]
		d = sorted(e)
		if e == d:
			print('YES')
		else:
			print('NO')
	else:
		d = c[::-1]
		e = sorted(c)
		if d == e:
			print('YES')
		else:
			print('NO')


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	idx = arr.index(n)
	flag = True
	ans1 = []
	ans2 = []
	for i in range(idx, -1, -1):
		ans1.append(arr[i])
	for i in range(n - 1, idx, -1):
		ans1.append(arr[i])
	for j in range(idx, n):
		ans2.append(arr[j])
	for j in range(0, idx):
		ans2.append(arr[j])
	k = [i for i in range(1, n + 1)]
	if ans1 == k or ans1 == k[::-1]:
		print('YES')
	elif ans2 == k or ans2 == k[::-1]:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	if len(l) == 1:
		print('YES')
	else:
		c = 0
		x = sorted(l)
		for j in range(n):
			if l != x:
				i = l[0]
				l.remove(l[0])
				l.append(i)
			else:
				c = 1
				break
		if c == 1:
			print('YES')
		else:
			c = 0
			x = sorted(l, reverse=True)
			for j in range(n):
				if l != x:
					i = l[-1]
					l.remove(l[-1])
					l.insert(0, i)
				else:
					c = 1
					break
			if c == 1:
				print('YES')
			else:
				print('NO')


def is_round(n, p):
	if n == 1:
		return 'YES'
	if p[0] == p[1] + 1 or (p[0] == 1 and p[n - 1] == 2):
		p = list(reversed(p))
	for i in range(1, n):
		if p[i] != p[i - 1] + 1 and (not (p[i] == 1 and p[i - 1] == n)):
			return 'NO'
	return 'YES'
q = int(input())
for i in range(q):
	n = int(input())
	p = [*map(int, input().split())]
	print(is_round(n, p))


t = int(input())
for i in range(t):
	n = int(input())
	p = list(map(int, input().split()))
	x = 0
	for i in range(n - 1):
		if abs(p[i] - p[i + 1]) == 1 or abs(p[i] - p[i + 1]) == n - 1:
			x += 1
	if x == n - 1:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split(' ')))
	mi = l.index(min(l))
	ma = l.index(max(l))
	ml = []
	if ma == mi + 1:
		k = mi
		while k != ma:
			ml.append(l[k])
			k -= 1
			if k == -1:
				k = n - 1
		ml.append(l[ma])
	if ma == mi - 1:
		k = mi
		while k != ma:
			ml.append(l[k])
			k += 1
			if k == n:
				k = 0
		ml.append(l[ma])
	ls = sorted(l)
	if ml == ls:
		print('YES')
	elif mi == 0 and ma == n - 1:
		if l == ls:
			print('YES')
		else:
			print('NO')
	elif mi == n - 1 and ma == 0:
		ls.reverse()
		if l == ls:
			print('YES')
		else:
			print('NO')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	array = [int(i) for i in input().split()]
	no = False
	for i in range(1, len(array)):
		if abs(array[i] - array[i - 1]) == 1 or abs(array[i] - array[i - 1]) == n - 1:
			no = False
		else:
			no = True
			break
	if not no:
		print('YES')
	else:
		print('NO')


for q in range(int(input())):
	n = int(input())
	b = list(map(int, input().split()))
	w = True
	k = b.index(1)
	a = b[k:] + b[:k]
	c = list(reversed(b))
	k = c.index(1)
	c = c[k:] + c[:k]
	if a == list(range(1, n + 1)) or c == list(range(1, n + 1)):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	n = int(input())
	students = list(map(int, input().split()))
	temp = students.copy()
	temp.sort()
	if students == temp:
		print('YES')
	else:
		test = students[::-1] + students[::-1]
		test2 = students + students
		x = test.index(1)
		y = test2.index(1)
		if test[x:x + n] == temp:
			print('YES')
		elif test2[y:y + n] == temp:
			print('YES')
		else:
			print('NO')


m = int(input())
for _ in range(m):
	n = int(input())
	c = [int(v) for v in input().split(' ')]
	a = c.index(1)
	r = True
	for i in range(n):
		if c[(a + i) % n] != i + 1:
			r = False
	if r:
		print('YES')
		continue
	r = True
	for i in range(n):
		if c[a - i] != i + 1:
			r = False
	if r:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	l = a.copy()
	r = a.copy()
	while l[0] != 1:
		temp = l[0]
		for j in range(n - 1):
			l[j] = l[j + 1]
		l[n - 1] = temp
	c = 1
	for k in range(1, n):
		if abs(l[k] - l[k - 1]) != 1:
			c = 0
			break
	if c == 1:
		print('YES')
	else:
		while r[n - 1] != 1:
			temp = r[n - 1]
			for j in range(n - 1, 0, -1):
				r[j] = r[j - 1]
			r[0] = temp
		for k in range(1, n):
			if abs(r[k] - r[k - 1]) != 1:
				c = 1
				print('NO')
				break
		if c == 0:
			print('YES')


n = int(input())
max_i = 0
b = []
v = []
t = []
for i in range(n):
	k = int(input())
	a = list(map(int, input().split()))
	for i in range(k):
		if a[i] == 1:
			min_i = i
	b = a[min_i:] + a[:min_i]
	a.reverse()
	for i in range(k):
		if a[i] == 1:
			min_i = i
	v = a[min_i:] + a[:min_i]
	a.sort()
	if v == a or b == a:
		print('YES')
	else:
		print('NO')
	min_i = 0


q = int(input())
for i in range(q):
	c = []
	b = []
	n = int(input())
	a = list(map(int, input().split()))
	for j in range(n):
		if a[j] == 1:
			c = a[j:] + a[:j]
			b = a[j + 1:] + a[:j + 1]
	if c == sorted(a):
		y = 'YES'
	else:
		a.sort(reverse=True)
		if b == a:
			y = 'YES'
		else:
			y = 'NO'
	print(y)


class cinn:

	def __init__(self):
		self.x = []

	def cin(self, t=int):
		if len(self.x) == 0:
			a = input()
			self.x = a.split()
			self.x.reverse()
		return self.get(t)

	def get(self, t):
		return t(self.x.pop())

	def clist(self, n, t=int):
		l = [0] * n
		for i in range(n):
			l[i] = self.cin(t)
		return l

	def clist2(self, n, t1=int, t2=int, t3=int, tn=2):
		l = [0] * n
		for i in range(n):
			if tn == 2:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				l[i] = (a1, a2)
			elif tn == 3:
				a1 = self.cin(t1)
				a2 = self.cin(t2)
				a3 = self.cin(t3)
				l[i] = (a1, a2, a3)
		return l

	def clist3(self, n, t1=int, t2=int, t3=int):
		return self.clist2(self, n, t1, t2, t3, 3)

	def cout(self, i, ans=''):
		if ans == '':
			print('Case #' + str(i + 1) + ':', end=' ')
		else:
			print('Case #' + str(i + 1) + ':', ans)

	def printf(self, thing):
		print(thing, end='')

	def countlist(self, l, s=0, e=None):
		if e == None:
			e = len(l)
		dic = {}
		for el in range(s, e):
			if l[el] not in dic:
				dic[l[el]] = 1
			else:
				dic[l[el]] += 1
		return dic

	def talk(self, x):
		print(x, flush=True)

	def dp1(self, k):
		L = [-1] * k
		return L

	def dp2(self, k, kk):
		L = [-1] * k
		for i in range(k):
			L[i] = [-1] * kk
		return L

	def isprime(self, n):
		if n == 1 or n == 0:
			return False
		for i in range(2, int(n ** 0.5 + 1)):
			if n % i == 0:
				return False
		return True

	def factors(self, n):
		from functools import reduce
		return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

	def nthprime(self, n):
		i = 0
		s = 2
		L = []
		while i < n:
			while not self.isprime(s):
				s += 1
			L.append(s)
			s += 1
			i += 1
		return L

	def matrixin(self, m, n, t=int):
		L = []
		for i in range(m):
			p = self.clist(n, t)
			L.append(p)
		return L

	def seive(self, k):
		n = k + 1
		L = [True] * n
		L[1] = False
		L[0] = False
		for i in range(2, n):
			if L[i] == True:
				for j in range(2 * i, n, i):
					L[j] = False
		return L

	def seiven(self, n, L):
		i = 0
		for j in range(len(L)):
			if L[j] == True:
				i += 1
			if i == n:
				return j

	def matrixin2(self, m, t=int):
		L = []
		for i in range(m):
			iny = self.cin(str)
			lsmall = []
			for el in iny:
				lsmall.append(t(el))
			L.append(lsmall)
		return L
c = cinn()
t = c.cin()
for el in range(t):
	n = c.cin()
	x = c.clist(n)
	o = x.index(1)
	o1 = x.index(n)
	cl = True
	ccl = True
	for i in range(o + 1, o + n):
		if x[i % n] != x[(i - 1) % n] + 1:
			cl = False
	for j in range(o1 + 1, o1 + n):
		if x[j % n] != x[(j - 1) % n] - 1:
			ccl = False
	if cl or ccl:
		print('YES')
	else:
		print('NO')


import sys
input = sys.stdin.readline

def query(n, A):
	idx_1 = P.index(1)
	P_1 = P[idx_1:] + P[:idx_1]
	if P_1 == list(range(1, n + 1)):
		return 'YES'
	idx_n = P.index(n)
	P_n = P[idx_n:] + P[:idx_n]
	if P_n == list(reversed(range(1, n + 1))):
		return 'YES'
	return 'NO'
q = int(input())
for _ in range(q):
	n = int(input())
	P = list(map(int, input().split()))
	print(query(n, P))


t = int(input())
for case_num in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	a.append(a[0])
	delta = (a[0] + n - a[1]) % n
	ok = True
	if delta != 1 and delta != n - 1:
		ok = False
	if ok:
		for i in range(1, n):
			if (a[i] + n - a[i + 1]) % n != delta:
				ok = False
				break
	print('YES' if ok else 'NO')


query = int(input())
while query > 0:
	askdljf = input()
	list1 = input().split()
	list1 = list(map(int, list1))
	decision = False
	x = sorted(list1)
	xr = sorted(list1)
	xr.reverse()
	for i in range(0, len(list1)):
		a = x.index(list1[i])
		ar = xr.index(list1[i])
		if list1[i - 1] == x[a - 1] or list1[i - 1] == xr[ar - 1]:
			decision = True
		elif list1[i - 1] != x[a - 1] and list1[i - 1] != xr[ar - 1]:
			decision = False
			break
	if decision == True:
		print('YES')
	else:
		print('NO')
	query -= 1


for _ in range(int(input())):
	n = int(input())
	p = list(map(int, input().split()))
	if n <= 3:
		print('YES')
	else:
		indl = p.index(1)
		indr = n - 1 - indl
		left = p[indl:] + p[0:indl]
		right = p[-indr:] + p[0:-indr]
		asc = sorted(p)
		desc = sorted(p, reverse=True)
		if left == asc or right == desc:
			print('YES')
		else:
			print('NO')


import sys

def inpt():
	return sys.stdin.readline().strip()

def check1(x):
	for i in range(len(x) - 1):
		if x[i + 1] - x[i] != 1 and x[i + 1] != 1:
			return False
	return True

def check2(x):
	for i in range(len(x) - 1):
		if x[i] - x[i + 1] != 1 and x[i + 1] != len(x):
			return False
	return True
q = int(inpt())
for i in range(q):
	n = int(inpt())
	x = [int(i) for i in inpt().split()]
	if check1(x) or check2(x):
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	is_clockwise = 1
	is_anticlockwise = 1
	for i in range(n - 1):
		if a[i + 1] - a[i] != 1:
			if a[i + 1] != 1:
				is_clockwise = 0
	for i in range(n - 1):
		if a[i + 1] - a[i] != -1:
			if a[i] != 1:
				is_anticlockwise = 0
	if is_anticlockwise == 1 or is_clockwise == 1:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	n = int(input())
	s = list(map(str, range(1, n + 1)))
	u = list(reversed(s))
	s = ' '.join(s)
	s += ' ' + s
	u = ' '.join(u)
	u += ' ' + u
	l = input().strip()
	print('YES' if l in s or l in u else 'NO')


for i in range(int(input())):
	n = int(input())
	p = [int(s) for s in input().split()]
	for i in range(n - 1):
		if not abs(p[i + 1] - p[i]) in [1, n - 1]:
			print('NO')
			break
	else:
		print('YES')