for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.append(a[0]) ok1 = True ok2 = False s = 0 for i in range(n): if a[i] == a[i + 1] != 1: ok1 = False if a[i] == 0: ok2 = True s += a[i] print("Yes" if ok1 and ok2 and s == n else "No")