#[allow(unused_imports)] use std::{ convert::{Infallible, TryFrom, TryInto as _}, fmt::{self, Debug, Display, Formatter,}, fs::File, hash::{Hash, Hasher, BuildHasherDefault}, iter::{Product, Sum}, marker::PhantomData, ops::{Add, AddAssign, Sub, SubAssign, Div, DivAssign, Mul, MulAssign, Neg, RangeBounds}, str::FromStr, sync::{atomic::{self, AtomicU32, AtomicU64}, Once}, collections::{*, btree_set::Range, btree_map::Range as BTreeRange}, mem::{swap}, cmp::{self, Reverse, Ordering, Eq, PartialEq, PartialOrd}, thread::LocalKey, f64::consts::PI, time::Instant, cell::RefCell, io::{self, stdin, Read, read_to_string, BufWriter, BufReader, stdout, Write}, }; pub mod fxhash { use std::hash::BuildHasherDefault; const K: u64 = 0x517c_c1b7_2722_0a95; #[derive(Default)] pub struct FxHasher { pub hash: u64, } impl FxHasher { #[inline(always)] fn mix_u64(mut h: u64, x: u64) -> u64 { h = h.rotate_left(5) ^ x; h = h.wrapping_mul(K); let x2 = x ^ (x >> 33) ^ (x << 11); h = h.rotate_left(5) ^ x2; h = h.wrapping_mul(K); h } #[inline(always)] fn write_u64_impl(&mut self, x: u64) { self.hash = Self::mix_u64(self.hash, x); } } impl std::hash::Hasher for FxHasher { #[inline(always)] fn finish(&self) -> u64 { self.hash } #[inline(always)] fn write(&mut self, bytes: &[u8]) { let mut h = self.hash; for &b in bytes { h = h.rotate_left(5) ^ (b as u64); h = h.wrapping_mul(K); } self.hash = h; } #[inline(always)] fn write_u64(&mut self, i: u64) { self.write_u64_impl(i); } #[inline(always)] fn write_u32(&mut self, i: u32) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_u16(&mut self, i: u16) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_u8 (&mut self, i: u8 ) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_usize(&mut self, i: usize) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_i64(&mut self, i: i64) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_i32(&mut self, i: i32) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_i16(&mut self, i: i16) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_i8 (&mut self, i: i8 ) { self.write_u64_impl(i as u64); } #[inline(always)] fn write_isize(&mut self, i: isize) { self.write_u64_impl(i as u64); } } pub type FxBuildHasher = BuildHasherDefault; pub type FxMap = std::collections::HashMap; pub type FxSet = std::collections::HashSet; } pub fn gcd(mut a: i64, mut b: i64)->i64{if a==0{return b;}else if b==0{return a;}let l1 = a.trailing_zeros();let l2 = b.trailing_zeros(); a >>= l1; b >>= l2;while a!=b{let x = (a^b).trailing_zeros();if a>x;}a << l1.min(l2)} pub fn factorial_i64(n: usize)->(Vec, Vec){ let mut res = vec![1; n+1];let mut inv = vec![1; n+1];for i in 0..n{ res[i+1] = (res[i]*(i+1)as i64)%MOD; } inv[n] = mod_inverse(res[n], MOD);for i in (0..n).rev(){ inv[i] = inv[i+1]*(i+1) as i64%MOD; }(res, inv) } pub fn floor(a:i64, b:i64)->i64{let res=(a%b+b)%b;(a-res)/b} pub fn modulo(a: i64, b: i64)->i64{(a%b+b)%b} pub fn extended_gcd(a:i64,b:i64)->(i64,i64,i64) {if b==0{(a,1,0)}else{let(g,x,y)=extended_gcd(b,a%b);(g,y,x-floor(a,b)*y)}} pub fn mod_inverse(a:i64,m:i64)->i64{let(_,x,_) =extended_gcd(a,m);(x%m+m)%m} pub fn comb(a: i64, b: i64, f: &Vec<(i64, i64)>)->i64{ if aVec<(i64, i64)>{ let mut f=vec![(1i64,1i64),(1, 1)];let mut z = 1i64; let mut inv = vec![0; x as usize+10];inv[1] = 1; for i in 2..x+1{z=(z*i)%MOD; let w=(MOD-inv[(MOD%i)as usize]*(MOD/i)%MOD)%MOD; inv[i as usize] = w; f.push((z, (f[i as usize-1].1*w)%MOD));}return f;} pub fn fast_mod_pow(mut x: i64,p: usize, m: i64)->i64{ x %= m; let mut res=1;let mut t=x;let mut z=p;while z > 0{ if z%2==1{res = (res*t)%m;}t = (t*t)%m;z /= 2; }res} pub trait SortD{ fn sort_d(&mut self); } impl SortD for Vec{ fn sort_d(&mut self) {self.sort_by(|u, v| v.cmp(&u));} } pub trait Mx{fn max(&self, rhs: Self)->Self;} impl Mx for f64{ fn max(&self, rhs: Self)->Self{if *self < rhs{ rhs } else { *self } }} pub trait Mi{ fn min(&self, rhs: Self)->Self; } impl Mi for f64{ fn min(&self, rhs: Self)->Self{ if *self > rhs{ rhs } else { *self } } } pub trait Chmax: PartialOrd + Copy {fn chmax(&mut self, rhs: Self) {if *self < rhs { *self = rhs; }}} impl Chmax for T {} pub trait Chmin: PartialOrd + Copy {fn chmin(&mut self, rhs: Self) {if *self > rhs { *self = rhs; }}} impl Chmin for T {} #[allow(unused)] use proconio::{*, marker::*}; #[allow(dead_code)] const INF: i64 = 1<<60; #[allow(dead_code)] const I: i32 = 1<<30; #[allow(dead_code)] const MOD: i64 = 998244353; #[allow(dead_code)] const D: [(usize, usize); 4] = [(1, 0), (0, 1), (!0, 0), (0, !0)]; #[allow(dead_code)] pub fn c2d(c: u8)->(usize, usize){match c{b'U'=>(!0,0),b'D'=>(1,0),b'L'=>(0,!0),b'R'=>(0,1),_=>unreachable!()}} #[allow(dead_code)] pub fn c2d_i64(c: u8)->(i64, i64){match c{b'U'=>(-1,0),b'D'=>(1,0),b'L'=>(0,-1),b'R'=>(0,1),_=>unreachable!()}} #[allow(dead_code)] const D2: [(usize, usize); 8] = [(1, 0), (1, 1), (0, 1), (!0, 1), (!0, 0), (!0, !0), (0, !0), (1, !0)]; pub trait SegtreeMonoid { type S: Clone; fn identity() -> Self::S; fn op(a: &Self::S, b: &Self::S) -> Self::S; } #[derive(Clone, Debug)] pub struct SegtreeNode{ val: S, left: u32, right: u32, } #[derive(Debug)] pub struct PersistentSegtree{ n: usize, data: Vec>, root: Vec, } impl PersistentSegtree { pub fn new(mut n: usize) -> Self { n = n.next_power_of_two(); let data = Vec::with_capacity(2*n); let mut sg = Self { n, data, root: Vec::new(), }; let r = sg.init(0, n); sg.root.push(r as u32); sg } pub fn new_with_q(mut n: usize, q: usize) -> Self { n = n.next_power_of_two(); let data = Vec::with_capacity(2*n+q*20); let mut sg = Self { n, data, root: Vec::new(), }; let r = sg.init(0, n); sg.root.push(r as u32); sg } pub fn build(a: &[M::S]) -> Self { let n = a.len().next_power_of_two(); let data = Vec::with_capacity(2*n); let mut sg = Self { n, data, root: Vec::new(), }; let r = sg.init_s(a, 0, n); sg.root.push(r as u32); sg } #[inline(always)] fn push_node(&mut self, node: SegtreeNode)->usize{ let r = self.data.len(); self.data.push(node); r } #[inline(always)] fn init(&mut self, l: usize, r: usize)->usize{ if l+1==r{ return self.push_node(SegtreeNode { val: M::identity(), left: !0, right: !0 }); } let m = (l+r)>>1; let left = self.init(l, m); let right = self.init(m, r); let val = M::op(&self.data[left].val, &self.data[right].val); self.push_node(SegtreeNode { val, left: left as u32, right: right as u32 }) } #[inline(always)] fn init_s(&mut self, a: &[M::S], l: usize, r: usize)->usize{ if l+1==r{ return self.push_node(SegtreeNode { val: if l < a.len(){a[l].clone()}else{M::identity()}, left: !0, right: !0 }); } let m = (l+r)>>1; let left = self.init_s(a, l, m); let right = self.init_s(a, m, r); let val = M::op(&self.data[left].val, &self.data[right].val); self.push_node(SegtreeNode { val, left: left as u32, right: right as u32 }) } #[inline] pub fn versions(&self) -> usize { self.root.len() } #[inline] pub fn update(&mut self, t: usize, p: usize, x: M::S){ let nr = self.update_dfs(self.root[t] as usize, 0, self.n, p, &x); self.root.push(nr as u32); } #[inline(always)] fn update_dfs(&mut self, cur: usize, l: usize, r: usize, p: usize, x: &M::S)->usize{ if l+1==r{ return self.push_node(SegtreeNode { val: x.clone(), left: !0, right: !0 }); } let m = (l+r)>>1; let pre = &self.data[cur]; let (cl, cr) = (pre.left, pre.right); let (nl, nr) = if p < m{ let nl = self.update_dfs(cl as usize, l, m, p, x) as u32; (nl, cr) } else { let nr = self.update_dfs(cr as usize, m, r, p, x)as u32; (cl, nr) }; self.push_node(SegtreeNode { val: M::op(&self.data[nl as usize].val, &self.data[nr as usize].val), left: nl, right: nr }) } #[inline] pub fn prod(&self, t: usize, l: usize, r: usize) -> M::S { self.prod_dfs(self.root[t]as usize, 0, self.n, l, r) } #[inline(always)] fn prod_dfs(&self, cur: usize, cl: usize, cr: usize, l: usize, r: usize) -> M::S { if r <= cl || cr <= l{ return M::identity(); } else if l <= cl && cr <= r { return self.data[cur].val.clone(); } let m = (cl+cr)/2; let node = &self.data[cur]; let ln = self.prod_dfs(node.left as usize, cl, m, l, r); let rn = self.prod_dfs(node.right as usize, m, cr, l, r); M::op(&ln, &rn) } #[inline] pub fn min_left(&self, t: usize, r: usize, f: F) -> usize where F: Fn(&M::S)->bool{ assert!(f(&M::identity())); if r==0{return 0;} let mut ac = M::identity(); self.min_left_dfs(self.root[t] as usize, 0, self.n, r, &mut ac, &f) } #[inline] pub fn max_right(&self, t: usize, l: usize, f: F) -> usize where F: Fn(&M::S)->bool{ assert!(f(&M::identity())); if l==self.n{return self.n;} let mut ac = M::identity(); self.max_right_dfs(self.root[t] as usize, 0, self.n, l, &mut ac, &f) } fn min_left_dfs(&self, cur: usize, l: usize, r: usize, x: usize, ac: &mut M::S, f: &F) -> usize where F: Fn(&M::S)->bool{ if x <= l {return l;} if r <= x{ let m = M::op(&self.data[cur].val, ac); if f(&m){ *ac = m; return l; } else if r-l==1{ return r; } } let m = (l+r)>>1; let node = &self.data[cur]; let ret = self.min_left_dfs(node.right as usize, m, r, x, ac, f); if ret > m{ return ret; } self.min_left_dfs(node.left as usize, l, m, x, ac, f) } fn max_right_dfs(&self, cur: usize, l: usize, r: usize, x: usize, ac: &mut M::S, f: &F) -> usize where F: Fn(&M::S)->bool{ if r <= x{return x;} if x <= l{ let m = M::op(ac, &self.data[cur].val); if f(&m){ *ac = m; return r; } if l+1==r{ return l; } } let m = (l+r)>>1; let node = &self.data[cur]; let (ln, rn) = (node.left, node.right); let ret = self.max_right_dfs(ln as usize, l, m, x, ac, f); if ret < m{ return ret; } self.max_right_dfs(rn as usize, m, r, x, ac, f) } pub fn get(&self, t: usize, p: usize) -> M::S { self.prod(t, p, p+1) } } struct M; impl SegtreeMonoid for M{ type S = u32; fn identity() -> Self::S { 1 } fn op(&a: &Self::S, &b: &Self::S) -> Self::S { ((a as i64*b as i64)%MOD)as u32 } } pub fn linear_sieve(mx: usize)->Vec{ let mut lpf = vec![0; mx+1]; lpf[1] = 1; let mut ps = Vec::new(); for i in 2..=mx{ if lpf[i]==0{ lpf[i] = i; ps.push(i); } for &p in &ps{ let ip = i*p; if ip>mx{break;} lpf[ip]=p; if p==lpf[i]{break;} } } lpf } pub fn calc_inverse(n: usize) -> Vec{let mut inv = vec![1; n+1]; for i in 2..=n{let div = MOD as i64/i as i64;inv[i] = (MOD-(inv[MOD as usize%i]as i64))%MOD*div%MOD;}inv} const MULTI: bool = false; #[fastout] fn solve(){ input!{ n: usize, a: [usize; n], q: usize, } const MX: usize = 200000; let lpf = linear_sieve(MX); let mut s = vec![Vec::<(usize, usize)>::new(); MX]; let mut seg = PersistentSegtree::::new(n); let mut ts = vec![0; n]; let mut now = vec![1; n]; let mut ch = Vec::with_capacity(n); let mut ti = 0; let inv = calc_inverse(MX); for (i, &v) in a.iter().enumerate().rev(){ let mut x = v; while x > 1{ let d = lpf[x]; let mut e = 0; while x%d==0{ x /= d; e += 1; } let mut ac = 0; while let Some((i, r))=s[d].pop(){ if ac+r >= e{ ch.push(i); now[i] = ((now[i]as i64*fast_mod_pow(inv[d], e-ac, MOD))%MOD)as u32; if ac+r > e{ s[d].push((i, ac+r-e)); } break; } else { ch.push(i); now[i] = ((now[i]as i64*fast_mod_pow(inv[d], r, MOD))%MOD)as u32; ac += r; } } s[d].push((i, e)); } now[i] = v as u32; ch.push(i);ch.sort_unstable();ch.dedup(); for &idx in &ch{ seg.update(ti, idx, now[idx]); ti += 1; } ts[i] = ti; ch.clear(); } let mut ans = 1; for _ in 0..q{ input!{ g: i64, h: i64, } let y = ((g*ans)%MOD)as usize%n+1; let w = ((h*ans)%MOD)as usize%n+1; let (l, r) = (y.min(w)-1, y.max(w)); let t = ts[l]; ans = seg.prod(t, l, r)as i64; println!("{}", ans); } } fn main() { if MULTI{ input!{ t: usize, } for _ in 0..t{ solve(); } } else { solve(); } }