import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random #sys.setrecursionlimit(10**9) #sys.set_int_max_str_digits(0) #alist = [] #s = input() t = int(input()) for _ in range(t): n = int(input()) alist = list(map(int,input().split())) if sum(alist) != n: print('No') continue if 2 not in alist: print('No') continue x = alist.index(2) blist = [None for i in range(n)] for i in range(n): if alist[x] == 2: blist[x] = '>' if alist[x] == 1: blist[x] = blist[x-1] if alist[x] == 0: blist[x] = '<' x += 1 x %= n c = 0 for i in range(n): if alist[i] != (blist[i] == '>') + (blist[i-1] == '<'): print('No') c = 1 break if c == 0: print('Yes')