#include #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; void solve() { int N; cin >> N; vector A(N); int S = 0; for(int i = 0; i < N; i++) { cin >> A[i]; S += A[i]; } bool fn = S == N, zero = false, two = false; for(int i = 0; i < N; i++) { if(A[i] == 2 && A[(i + 1) % N] == 2) { fn = false; } if(A[i] == 0 && A[(i + 1) % N] == 0) { fn = false; } if(A[i] == 2 && A[(i + 1) % N] == 1 && A[(i + 2) % N] == 2) { fn = false; } if(A[i] == 0) { zero = true; } if(A[i] == 2) { two = true; } } if(!fn || !zero || !two) { cout << "No\n"; } else { cout << "Yes\n"; } } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int T; cin >> T; while(T--) { solve(); } return 0; }