#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // 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),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($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; } } } /** * Sparse Table. * BiOp should be the type of a binary operator which is * associative, commutative and idempotent. * (For example, both min and gcd satisfy these properties.) * Verified by: AtCoder CODE FESTIVAL 2016 Tournament Round 3 (Parallel) B * (http://cf16-tournament-round3-open.contest.atcoder.jp/submissions/1026294) */ struct SparseTable { biop: BiOp, st: Vec>, } impl SparseTable where BiOp: Fn(T, T) -> T, T: Copy { pub fn new(ary: &[T], biop: BiOp) -> Self { let n = ary.len(); let mut h = 1; while 1 << h < n { h += 1; } let mut st: Vec> = vec![Vec::from(ary); h + 1]; for i in 0 .. n { st[0][i] = ary[i]; } for b in 1 .. (h + 1) { if n + 1 < 1 << b { break; } for i in 0 .. (n + 1 - (1 << b)) { let next_idx = (1 << (b - 1)) + i; st[b][i] = biop(st[b - 1][i], st[b - 1][next_idx]); } } SparseTable {biop: biop, st: st} } fn top_bit(t: usize) -> usize { 8 * std::mem::size_of::() - 1 - t.leading_zeros() as usize } pub fn query(&self, range: std::ops::Range) -> T { let (f, s) = (range.start, range.end - 1); assert!(f <= s); let b = Self::top_bit(s + 1 - f); let endpoint = s + 1 - (1 << b); (self.biop)(self.st[b][f], self.st[b][endpoint]) } } // https://yukicoder.me/problems/no/2171 (3) // 各 i に対して最終的な A_i の値として考えられるのは or(A_j, ..., A_i) の形の値なので 30 通り程度。それぞれに対してどこを左端とするかをあらかじめ計算しておく。 // 特定のパターンがあり得ることと、A_i の値が or(A_j, ..., A_i) であるような最大の j を L_i としたとき L_i <= L_{i+1} が成立することが同値である。よって DP でできる。 // W = 30 として O(NW^2)-time である。 fn main() { input! { n: usize, a: [u32; n], } let mut pts = vec![vec![]; n]; let mut last = vec![]; let spt = SparseTable::new(&a, |a, b| a | b); for i in 0..n { last.push(i); for val in &mut last { // right-shift val as much as possible while *val < i && spt.query(*val..i + 1) == spt.query(*val + 1..i + 1) { *val += 1; } } last.dedup(); pts[i] = last.clone(); } const MOD: i64 = 998_244_353; let mut dp = vec![1]; for i in 1..n { let mut ep = vec![0; pts[i].len()]; for j in 0..pts[i - 1].len() { for k in 0..pts[i].len() { if pts[i - 1][j] <= pts[i][k] { ep[k] += dp[j]; if ep[k] >= MOD { ep[k] -= MOD; } } } } dp = ep; } println!("{}", dp.iter().sum::() % MOD); }