#include using namespace std; typedef signed long long ll; #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define FORR2(x,y,arr) for(auto& [x,y]:arr) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) template bool chmax(T &a, const T &b) { if(a bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;} //------------------------------------------------------- int T,N; int A[505050]; int iskado(int cur) { int L=(N+cur-1)%N; int R=(cur+1)%N; if(cur<0||cur>=N) return 0; if(cur==0||cur==N-1) return 1; if(A[cur]==A[L]) return 0; if(A[cur]==A[R]) return 0; if(A[L]==A[R]) return 0; return (A[L]>T; while(T--) { cin>>N; FOR(i,N) cin>>A[i]; vector cand; FOR(i,N) if(iskado(i)==0) cand.push_back(i); if(cand.size()>6) { cout<<"No"< S; S.insert(i-1); S.insert(i); S.insert(i+1); S.insert(c-1); S.insert(c); S.insert(c+1); FORR(s,S) cur-=iskado(s); swap(A[i],A[c]); FORR(s,S) cur+=iskado(s); if(cur==N) ret=1; swap(A[i],A[c]); } } if(ret) cout<<"Yes"<