#include #define rep(i, n) for(int i = 0; i < (int)(n); ++i) #define rep1(i, n) for(int i = 1; i <= (int)(n); ++i) #define memset(arr, x) memset(arr, x, sizeof(arr)) using namespace std; using i64 = long long; constexpr int inf = 1.01e9; constexpr i64 llinf = 1.01e18; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } template inline bool read (A &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; return 1; } inline bool read (char &x) { while((x=gc())==' '||x=='\n' || x=='\r'); return x!=EOF; } inline bool read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); if(*x==EOF)return 0; while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc(); *x=0; return 1; } template inline bool read(A &x,B &...y){ return read(x)&&read(y...); } template inline bool write (A x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); return 0; } inline bool write (char x) { putc(x); return 0; } inline bool write(const char *x){ while(*x){putc(*x);++x;} return 0; } inline bool write(char *x){ while(*x){putc(*x);++x;} return 0; } template inline bool write(A x,B ...y){ return write(x)||write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; void solve(){ int n; read(n); vector a(n); int cnt = 0; rep(i, n){ read(a[i]); if(a[i]) cnt++; } int cnt2 = n - cnt, x = 0, y = 0; rep(i, n){ if(a[i] == 0){ x++; while(i < n && a[i] == 0) i++; --i; }else{ y++; while(i < n && a[i] == 1) i++; --i; } } write((cnt >= x && y <= x) || (cnt2 >= y && x <= cnt) ? "Yes\n" : "No\n"); } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tt = 1; read(tt); rep(i, tt) solve(); }