use std::vec; 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, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } #[derive(Debug, Clone, Copy)] struct ModInt { value: usize, } impl ModInt { fn modulo() -> usize { 998244353 } fn new(value: usize) -> Self { Self { value: value % Self::modulo(), } } fn pow(self, n: usize) -> Self { let mut res = Self::new(1); let mut x = self; let mut n = n; while n > 0 { if n & 1 == 1 { res = res * x; } x = x * x; n >>= 1; } res } } impl std::ops::Add for ModInt { type Output = Self; fn add(self, rhs: Self) -> Self { Self::new(self.value + rhs.value) } } impl std::ops::Sub for ModInt { type Output = Self; fn sub(self, rhs: Self) -> Self { Self::new(self.value + Self::modulo() - rhs.value) } } impl std::ops::Mul for ModInt { type Output = Self; fn mul(self, rhs: Self) -> Self { Self::new(self.value * rhs.value) } } impl std::ops::Div for ModInt { type Output = Self; fn div(self, rhs: Self) -> Self { self * rhs.pow(Self::modulo() - 2) } } fn main() { input! { n: usize, m: usize, q: usize, a: [usize; n], query: [(usize, usize); q], } let m = ModInt::new(m); let m_pow = (0..n).map(|i| m.pow(i)).collect::>(); let b = (0..n) .map(|i| ModInt::new(a[i] - 1) * m_pow[n - i - 1]) .collect::>(); let cs = { let mut cs = vec![ModInt::new(0); 1]; for &b in &b { cs.push(cs[cs.len() - 1] + b); } cs }; for (l, r) in query { let l = l - 1; let sum = cs[r] - cs[l]; let ans = sum / m_pow[n - r] + ModInt::new(1); println!("{}", ans.value); } }