let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let count a b c k =
        (if a = k then 1 else 0) + (if b = k then 1 else 0) + (if c = k then 1 else 0)
    in
    let m = 1000000007 in
    let ( *@) a b = ((a mod m) * (b mod m)) mod m in
    let main () =
        let n = int_of_string (read_line ()) in
        let rec loop r g b acc = function
            | [] -> acc
            | hd :: tl ->
                    let acc = acc *@ count r g b hd in
                    let r, g, b = if r = hd then r + 1, g, b else
                                  if g = hd then r, g + 1, b else
                                                 r, g, b + 1
                    in
                    loop r g b acc tl
        in
        let a = split (read_line ()) in
        let q = loop 0 0 0 1 a in
        Printf.printf "%d\n" q
    in
    main ()