let n = read_int ()
let h n = Array.init n @@ fun _ -> Scanf.scanf " %d" (+) 0
let ps = h n
let qs = h n
let fs = [|1; 1; 2; 6; 24; 120; 720; 5040|]
let g n us = let c = ref 0 in for i = 1 to n do if not us.(i) then incr c done; !c
let f a = let c, us = ref 1, Array.make 10 false in for i = 1 to n - 1 do c := !c + g (a.(i - 1) - 1) us * fs.(n - i); us.(a.(i - 1)) <- true done; !c
let _ = Printf.printf "%d\n" @@ abs (f ps - f qs)