let ans, n = ref 1, read_int ()
let us, a_s = Array.(make (n + 1) 0, init n @@ fun _ -> Scanf.scanf " %d" (+) 0)
let _ = us.(0) <- 3; Array.iter (fun a -> ans := !ans * us.(a) mod 1000000007; us.(a) <- us.(a) - 1; if us.(a) < 0 then (print_endline "0"; exit 0); us.(a + 1) <- us.(a + 1) + 1) a_s; Printf.printf "%d\n" !ans