use std::io::Read; fn get_word() -> String { let stdin = std::io::stdin(); let mut stdin=stdin.lock(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } /// Verified by https://atcoder.jp/contests/abc198/submissions/21774342 mod mod_int { use std::ops::*; pub trait Mod: Copy { fn m() -> i64; } #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct ModInt { pub x: i64, phantom: ::std::marker::PhantomData } impl ModInt { // x >= 0 pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) } fn new_internal(x: i64) -> Self { ModInt { x: x, phantom: ::std::marker::PhantomData } } pub fn pow(self, mut e: i64) -> Self { debug_assert!(e >= 0); let mut sum = ModInt::new_internal(1); let mut cur = self; while e > 0 { if e % 2 != 0 { sum *= cur; } cur *= cur; e /= 2; } sum } #[allow(dead_code)] pub fn inv(self) -> Self { self.pow(M::m() - 2) } } impl Default for ModInt { fn default() -> Self { Self::new_internal(0) } } impl>> Add for ModInt { type Output = Self; fn add(self, other: T) -> Self { let other = other.into(); let mut sum = self.x + other.x; if sum >= M::m() { sum -= M::m(); } ModInt::new_internal(sum) } } impl>> Sub for ModInt { type Output = Self; fn sub(self, other: T) -> Self { let other = other.into(); let mut sum = self.x - other.x; if sum < 0 { sum += M::m(); } ModInt::new_internal(sum) } } impl>> Mul for ModInt { type Output = Self; fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) } } impl>> AddAssign for ModInt { fn add_assign(&mut self, other: T) { *self = *self + other; } } impl>> SubAssign for ModInt { fn sub_assign(&mut self, other: T) { *self = *self - other; } } impl>> MulAssign for ModInt { fn mul_assign(&mut self, other: T) { *self = *self * other; } } impl Neg for ModInt { type Output = Self; fn neg(self) -> Self { ModInt::new(0) - self } } impl ::std::fmt::Display for ModInt { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { self.x.fmt(f) } } impl From for ModInt { fn from(x: i64) -> Self { Self::new(x) } } } // mod mod_int macro_rules! define_mod { ($struct_name: ident, $modulo: expr) => { #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct $struct_name {} impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } } } const MOD: i64 = 998_244_353; define_mod!(P, MOD); type MInt = mod_int::ModInt

; // Lazy Segment Tree. This data structure is useful for fast folding and updating on intervals of an array // whose elements are elements of monoid T. Note that constructing this tree requires the identity // element of T and the operation of T. This is monomorphised, because of efficiency. T := i64, biop = max, upop = (+) // Reference: https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp // Verified by: https://judge.yosupo.jp/submission/68794 // https://atcoder.jp/contests/joisc2021/submissions/27734236 pub trait ActionRing { type T: Clone + Copy; // data type U: Clone + Copy + PartialEq + Eq; // action fn biop(x: Self::T, y: Self::T) -> Self::T; fn update(x: Self::T, a: Self::U) -> Self::T; fn upop(fst: Self::U, snd: Self::U) -> Self::U; fn e() -> Self::T; fn upe() -> Self::U; // identity for upop } #[derive(Clone)] pub struct LazySegTree { n: usize, dep: usize, dat: Vec, lazy: Vec, } impl LazySegTree { pub fn new(n_: usize) -> Self { let mut n = 1; let mut dep = 0; while n < n_ { n *= 2; dep += 1; } // n is a power of 2 LazySegTree { n: n, dep: dep, dat: vec![R::e(); 2 * n], lazy: vec![R::upe(); n], } } #[allow(unused)] pub fn with(a: &[R::T]) -> Self { let mut ret = Self::new(a.len()); let n = ret.n; for i in 0..a.len() { ret.dat[n + i] = a[i]; } for i in (1..n).rev() { ret.update_node(i); } ret } #[inline] pub fn set(&mut self, idx: usize, x: R::T) { debug_assert!(idx < self.n); self.apply_any(idx, |_t| x); } #[inline] pub fn apply(&mut self, idx: usize, f: R::U) { debug_assert!(idx < self.n); self.apply_any(idx, |t| R::update(t, f)); } pub fn apply_any R::T>(&mut self, idx: usize, f: F) { debug_assert!(idx < self.n); let idx = idx + self.n; for i in (1..self.dep + 1).rev() { self.push(idx >> i); } self.dat[idx] = f(self.dat[idx]); for i in 1..self.dep + 1 { self.update_node(idx >> i); } } pub fn get(&mut self, idx: usize) -> R::T { debug_assert!(idx < self.n); let idx = idx + self.n; for i in (1..self.dep + 1).rev() { self.push(idx >> i); } self.dat[idx] } /* [l, r) (note: half-inclusive) */ #[inline] pub fn query(&mut self, l: usize, r: usize) -> R::T { debug_assert!(l <= r && r <= self.n); if l == r { return R::e(); } let mut l = l + self.n; let mut r = r + self.n; for i in (1..self.dep + 1).rev() { if ((l >> i) << i) != l { self.push(l >> i); } if ((r >> i) << i) != r { self.push((r - 1) >> i); } } let mut sml = R::e(); let mut smr = R::e(); while l < r { if (l & 1) != 0 { sml = R::biop(sml, self.dat[l]); l += 1; } if (r & 1) != 0 { r -= 1; smr = R::biop(self.dat[r], smr); } l >>= 1; r >>= 1; } R::biop(sml, smr) } /* ary[i] = upop(ary[i], v) for i in [l, r) (half-inclusive) */ #[inline] pub fn update(&mut self, l: usize, r: usize, f: R::U) { debug_assert!(l <= r && r <= self.n); if l == r { return; } let mut l = l + self.n; let mut r = r + self.n; for i in (1..self.dep + 1).rev() { if ((l >> i) << i) != l { self.push(l >> i); } if ((r >> i) << i) != r { self.push((r - 1) >> i); } } { let l2 = l; let r2 = r; while l < r { if (l & 1) != 0 { self.all_apply(l, f); l += 1; } if (r & 1) != 0 { r -= 1; self.all_apply(r, f); } l >>= 1; r >>= 1; } l = l2; r = r2; } for i in 1..self.dep + 1 { if ((l >> i) << i) != l { self.update_node(l >> i); } if ((r >> i) << i) != r { self.update_node((r - 1) >> i); } } } #[inline] fn update_node(&mut self, k: usize) { self.dat[k] = R::biop(self.dat[2 * k], self.dat[2 * k + 1]); } fn all_apply(&mut self, k: usize, f: R::U) { self.dat[k] = R::update(self.dat[k], f); if k < self.n { self.lazy[k] = R::upop(self.lazy[k], f); } } fn push(&mut self, k: usize) { let val = self.lazy[k]; self.all_apply(2 * k, val); self.all_apply(2 * k + 1, val); self.lazy[k] = R::upe(); } } #[derive(Clone)] enum Affine {} type AffineInt = MInt; // Change here to change type impl ActionRing for Affine { type T = (AffineInt, AffineInt); // data, size type U = (AffineInt, AffineInt); // action, (a, b) |-> x |-> ax + b fn biop((x, s): Self::T, (y, t): Self::T) -> Self::T { (x + y, s + t) } fn update((x, s): Self::T, (a, b): Self::U) -> Self::T { (x * a + b * s, s) } fn upop(fst: Self::U, snd: Self::U) -> Self::U { let (a, b) = fst; let (c, d) = snd; (a * c, b * c + d) } fn e() -> Self::T { (0.into(), 0.into()) } fn upe() -> Self::U { // identity for upop (1.into(), 0.into()) } } #[allow(dead_code)] fn get() -> T { get_word().parse().ok().unwrap() } fn upd(n: usize, st: &mut LazySegTree, l: usize, r: usize, x: MInt) { if l <= r { st.update(l, r + 1, (0.into(), x)); } else { st.update(l, n, (0.into(), x)); st.update(0, r + 1, (0.into(), x)); } } fn que(n: usize, st: &mut LazySegTree, l: usize, r: usize) -> MInt { if l <= r { st.query(l, r + 1).0 } else { let a = st.query(l, n).0; let b = st.query(0, r + 1).0; a + b } } // https://yukicoder.me/problems/no/1548 (3) // サイクルの中の範囲の 1 乗和から 4 乗和が分かれば良いので、遅延セグメント木を 4 本持てば良い。 fn main() { let n: usize = get(); let a: Vec = (0..n).map(|_| get()).collect(); let q: usize = get(); let mut st = vec![LazySegTree::::new(n); 4]; for i in 0..n { let b = MInt::new(a[i]); let mut c = b; for j in 0..4 { st[j].set(i, (c, 1.into())); c *= b; } } let comb = vec![ vec![1], vec![1, 1], vec![1, 2, 1], vec![1, 3, 3, 1], vec![1, 4, 6, 4, 1], ]; for _ in 0..q { let ty: usize = get(); let u = get::() - 1; let v = get::() - 1; let w = get::() - 1; let (u, v) = if u > v { (v, u) } else { (u, v) }; let (l, r) = if u < w && w < v { (u, v) } else { (v, u) }; if ty == 0 { let b: i64 = get(); let b = MInt::new(b); let mut c = b; for i in 0..4 { upd(n, &mut st[i], l, r, c); c *= b; } } else { let len = (r + n - l) % n + 1; let len = len as i64; let leninv = MInt::new(len).inv(); let mut ans = MInt::new(0); let avg = -que(n, &mut st[0], l, r) * leninv; let mut c = MInt::new(1); for i in (0..ty + 1).rev() { let tmp = if i == 0 { MInt::new(len) } else { que(n, &mut st[i - 1], l, r) } * comb[ty][i] * c; ans += tmp; c *= avg; } println!("{}", ans * leninv); } } }