use std::cmp::Reverse; const MOD: usize = 1e9 as usize + 7; #[derive(PartialEq, PartialOrd, Clone, Debug)] struct Sortable(T); impl Eq for Sortable {} impl Ord for Sortable { fn cmp(&self, other: &Sortable) -> std::cmp::Ordering { self.0.partial_cmp(&other.0).unwrap() } } fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut a = String::new(); std::io::stdin().read_line(&mut a).ok(); let a: Vec = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut b = String::new(); std::io::stdin().read_line(&mut b).ok(); let b: Vec = b.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut orders = (0..n).collect::>(); orders.sort_by_key(|&i| Reverse(Sortable((b[i] - 1) as f64 / a[i] as f64))); let mut result = 0; let mut level = 1usize; for &i in orders.iter() { result += level * a[i] % MOD; result %= MOD; level = level * b[i] % MOD; } println!("{}", result); }