Scanf.scanf "%d" (fun n -> let a = Array.init n (fun _ -> Scanf.scanf " %d" (fun a -> a)) in let b = Array.init n (fun _ -> Scanf.scanf " %d" (fun b -> b)) in let ab = Array.init n (fun i -> a.(i), b.(i)) in Array.sort (fun (a1, b1) (a2, b2) -> compare ((b1 - 1) * a2) ((b2 - 1) * a1)) ab; let z = 1000000007 in let ( +@) a b = (a + b) mod z in let ( *@) a b = (a * b) mod z in let rec loop i bl acc = if i < 0 then acc else let a, b = ab.(i) in loop (i - 1) (bl *@ b) (acc +@ bl *@ a) in loop (n - 1) 1 0 |> Printf.printf "%d\n" )