#![allow(non_snake_case, unused_imports, unused_must_use)] use std::io::{self, prelude::*}; use std::str; fn main() { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); macro_rules! input { ($T: ty) => { scan.token::<$T>() }; ($T: ty, $N: expr) => { (0..$N).map(|_| scan.token::<$T>()).collect::>() }; } for _ in 0..input!(usize) { let N = input!(usize); let A = input!(usize, N); let s = A.iter().sum::(); if s != N { writeln!(out, "No"); continue; } let B = { let mut b = vec![]; for a in A { if a != 1 { b.push(a); } } b }; if B.is_empty() { writeln!(out, "No"); continue; } let mut isok = true; let L = B.len(); for i in 0..L { let lhs = { if i == 0 { B[L - 1] } else { B[i - 1] } }; let rhs = { if i == L - 1 { B[0] } else { B[i + 1] } }; if B[i] == 0 { if !(lhs > B[i] && rhs > B[i]) { isok = false; } } else { if !(lhs < B[i] && rhs < B[i]) { isok = false; } } } if isok { writeln!(out, "Yes"); } else { writeln!(out, "No"); } } } struct Scanner { reader: R, buf_str: Vec, buf_iter: str::SplitWhitespace<'static>, } impl Scanner { fn new(reader: R) -> Self { Self { reader, buf_str: vec![], buf_iter: "".split_whitespace(), } } fn token(&mut self) -> T { loop { if let Some(token) = self.buf_iter.next() { return token.parse().ok().expect("Failed parse"); } self.buf_str.clear(); self.reader .read_until(b'\n', &mut self.buf_str) .expect("Failed read"); self.buf_iter = unsafe { let slice = str::from_utf8_unchecked(&self.buf_str); std::mem::transmute(slice.split_whitespace()) } } } }