use std::io::{self, Read as _, Write as _}; struct Scanner<'a>(std::str::SplitWhitespace<'a>); impl<'a> Scanner<'a> { fn new(s: &'a str) -> Self { Self(s.split_whitespace()) } fn next(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, { let s = self.0.next().expect("found EOF"); match s.parse() { Ok(v) => v, Err(msg) => { println!( "parse error. T = {}, s = \"{}\": {:?}", std::any::type_name::(), s, msg ); panic!() } } } } mod fp { use std::convert::From; use std::ops; const P: u32 = 1000000007; #[derive(Copy, Clone)] pub struct Fp(pub u32); impl From for Fp { fn from(mut x: i64) -> Fp { x %= P as i64; if x < 0 { x += P as i64; } Fp(x as u32) } } impl ops::Add for Fp { type Output = Fp; fn add(mut self, rhs: Fp) -> Fp { self += rhs; self } } impl ops::AddAssign for Fp { fn add_assign(&mut self, rhs: Fp) { self.0 += rhs.0; if self.0 >= P { self.0 -= P; } } } impl ops::Mul for Fp { type Output = Fp; fn mul(mut self, rhs: Fp) -> Fp { self *= rhs; self } } impl ops::MulAssign for Fp { fn mul_assign(&mut self, rhs: Fp) { self.0 = (self.0 as u64 * rhs.0 as u64 % P as u64) as u32; } } impl ops::Neg for Fp { type Output = Fp; fn neg(self) -> Fp { Fp(match self.0 { 0 => 0, s => P - s, }) } } impl ops::Sub for Fp { type Output = Fp; fn sub(mut self, rhs: Fp) -> Fp { self -= rhs; self } } impl ops::SubAssign for Fp { fn sub_assign(&mut self, rhs: Fp) { if self.0 < rhs.0 { self.0 += P; } self.0 -= rhs.0; } } impl std::iter::Sum for Fp { fn sum(iter: I) -> Fp where I: Iterator, { iter.fold(Fp(0), |s, x| s + x) } } } use fp::Fp; fn main() { let mut stdin = String::new(); std::io::stdin().read_to_string(&mut stdin).unwrap(); let mut sc = Scanner::new(&stdin); let stdout = io::stdout(); let mut stdout = io::BufWriter::new(stdout.lock()); let k = sc.next(); let n = sc.next(); let m = sc.next(); let mut a = (0..k).map(|_| Fp(sc.next())).collect::>(); let c = (0..k) .map(|_| Fp(sc.next())) .collect::>() .into_iter() .rev() .collect::>(); let mut ls = vec![Vec::new(); n + 1]; let mut rs = vec![Vec::new(); n + 1]; for _ in 0..m { let (l, r): (usize, usize) = (sc.next(), sc.next()); ls[l].push(0); rs[r].push(r - l); } a.reserve_exact(n); for i in 0..n { a.push(c.iter().zip(&a[i..]).map(|(&c, &a)| c * a).sum()); } let mut state = vec![Fp(0); k]; for (ls, rs) in ls.into_iter().zip(rs).take(n) { { let next = c.iter().zip(&state).map(|(&c, &s)| c * s).sum(); state.remove(0); state.push(next); } for l in ls { for (s, &a) in state.iter_mut().zip(&a[l..]) { *s += a; } } for l in rs { for (s, &a) in state.iter_mut().zip(&a[l..]) { *s -= a; } } writeln!(stdout, "{}", state[0].0).unwrap(); } stdout.flush().unwrap(); }