let rec fix f x = f (fix f) x
let ( *@) a b = a * b mod 1000000007
let _ =
  Scanf.scanf "%d" @@ fun n ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  let hats = [|0; 0; 0|] in
  (1, 0) |> fix (fun f (d, i) ->
    if i >= n then d else begin
      let c = Array.fold_left (fun c h -> if a.(i) = h then c+1 else c) 0 hats in
      0 |> fix (fun f j ->
        if j >= 3 then ()
        else if hats.(j) = a.(i) then hats.(j) <- hats.(j) + 1
        else f (j+1));
      f (c *@ d, i+1)
    end)
  |> Printf.printf "%d\n"


