#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::>() }; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } const MOD: i64 = 1_000_000_007; fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); } fn solve() { input! { n: usize, a: [i64; n], } let inf = 400_001; let mut one = true; let mut l = 0; let mut v = vec![]; let mut c = 1; let mut d = 1; for i in 0..n { if one != (a[i] == 1) { v.push((l, c, d)); one = a[i] == 1; l = 1; c = 1; d = 1; } else { c = min(inf, c * a[i]); d = d * a[i] % MOD; l += 1; } } v.push((l, c, d)); if v.len() % 2 == 0 { v.push((0, 1, 1)); } let m = v.len() / 2; if m == 0 { println!("{}", v[0].0); return; } c = 1; d = 1; for i in 0..m { let (_, cc, dd) = v[2 * i + 1]; c = min(inf, c * cc); d = d * dd % MOD; } if c >= inf { println!("{}", (d + v[0].0 + v[2 * m].0) % MOD); return; } let mut dp = vec![0; m + 1]; for i in 0..m { let mut ans = 0; for j in 0..i + 1 { let mut p = 1; for k in j..i + 1 { p *= v[2 * k + 1].1; } ans = max(ans, v[2 * j].0 + p); } dp[i + 1] = ans; } println!("{}", dp[m] + v[2 * m].0); }