for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) if 2 not in A: print("No") continue idx = A.index(2) D = [None for _ in range(n)] for i in range(n): j = (i + idx) % n if A[j] == 2: to = '>' elif A[j] == 0: to = '<' else: to = D[(j - 1) % n] D[j] = to flag = True for i in range(n): if A[i] != (D[(i - 1) % n] == '<') + (D[i] == '>'): flag = False break if flag: print("Yes") else: print("No")