def solve(aa: list[int]): n = len(aa) zero_num = aa.count(0) two_num = aa.count(2) if ( zero_num == 0 or zero_num > n // 2 or two_num == 0 or two_num > n // 2 or zero_num != two_num ): return False start = aa.index(0) inc = False for i in range(1, n): cur = (start + i) % n if inc: if aa[cur] == 2: return False if aa[cur] == 0: inc = False else: if aa[cur] == 0: return False if aa[cur] == 2: inc = True return True def main(): t = int(input()) for _ in range(t): input() aa = list(map(int, input().split())) print("Yes" if solve(aa) else "No") if __name__ == "__main__": main()