let n = read_int ()
let h n = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let ps = h n
let qs = h n
let f n = [|1; 1; 2; 6; 24; 120; 720; 5040; 40320; 362880|].(n)
let g xs = Array.(let fs = make n true in fold_left (fun (i, s) x -> fs.(x - 1) <- false; let c = ref 0 in iteri (fun j b -> if b && j + 1 < x then incr c) fs; i + 1, s + !c * f (n - i)) (1, 0) xs |> snd)
let _ = Printf.printf "%d\n" @@ abs (g ps - g qs)