let m = 1000000007
let ( +^ ) x y = (x + y) mod m
let ( *^ ) x y = (x * y) mod m

module IntTripleMap = Map.Make (struct
  type t = int * int * int
  let compare = compare
end)

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun a -> a in
  let m =
    Array.fold_left (fun m a ->
      IntTripleMap.fold (fun (r, g, b) n m ->
        let m =
          if r <> a then m else 
          IntTripleMap.add (r + 1, g, b) 
          (n +^
            try IntTripleMap.find (r + 1, g, b) m
            with Not_found -> 0) m in
        let m =
          if g <> a then m else 
          IntTripleMap.add (r, g + 1, b) 
            (n +^
              try IntTripleMap.find (r, g + 1, b) m
              with Not_found -> 0) m in
        let m =
          if b <> a then m else 
          IntTripleMap.add (r, g, b + 1) 
            (n +^
              try IntTripleMap.find (r, g, b + 1) m
              with Not_found -> 0) m in
        m) m IntTripleMap.empty) (IntTripleMap.singleton (0, 0, 0) 1) as_ in
  Printf.printf "%d\n" @@
  IntTripleMap.fold (fun _ -> ( +^ )) m 0

