Problem:
Score: 500 points

Problem Statement

N people are standing in a queue, numbered 1, 2, 3, ..., N from front to back. Each person wears a hat, which is red, blue, or green.

The person numbered i says:

"In front of me, exactly A_i people are wearing hats with the same color as mine."

Assuming that all these statements are correct, find the number of possible combinations of colors of the N people's hats.

Since the count can be enormous, compute it modulo 1000000007.

Constraints

1 \leq N \leq 100000

0 \leq A_i \leq N-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_N

Output

Print the number of possible combinations of colors of the N people's hats, modulo 1000000007.

Sample Input 1

6
0 1 2 3 4 5

Sample Output 1

3

We have three possible combinations, as follows:

Red, Red, Red, Red, Red, Red

Blue, Blue, Blue, Blue, Blue, Blue

Green, Green, Green, Green, Green, Green

Sample Input 2

3
0 0 0

Sample Output 2

6

Sample Input 3

54
0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17

Sample Output 3

115295190

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace A
{
    class StartingPoint
    {
        static void Main(string[] args)
        {
            if (args?.Any() == true)
            {
                var assembly = Assembly.GetEntryAssembly();
                var resourceName = "AtCoderCSharp.In.txt";
                var stream = assembly.GetManifestResourceStream(resourceName);
                var streamReader = new StreamReader(stream, Encoding.UTF8);
                var str = streamReader.ReadToEnd();
                var textReader = new StringReader(str);
                Console.SetIn(textReader);
            }

            try
            {
                foreach (var s in new Program().Solve())
                {
                    Console.WriteLine(s);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                if (args?.Any() != true)
                    throw e;
            }

            if (args?.Any() == true)
                Console.ReadKey();
        }
    }

    class Program
    {
        public List<int> ScanInts(int count) => Enumerable.Range(0, count).Select(_ => ScanInt).ToList();
        public List<int> ScanInts(long count) => ScanInts((int)count);
        public List<long> ScanLongs(int count) => Enumerable.Range(0, count).Select(_ => Scan).ToList();
        public List<long> ScanLongs(long count) => ScanLongs((int)count);
        public int ScanInt => int.Parse(Str);
        public long Scan => long.Parse(Str);
        public double ScanDouble => (double)Scan;

        StringBuilder sb = new StringBuilder();

        public string Str => ScanStr();
        public string ScanStr()
        {
            sb.Clear();

            int r = 0;
            bool? b = null;
            do
            {
                r = Console.Read();
                b = IsBreak(r);
            } while (b == true);

            if (b == null)
                throw new Exception("input error: unexpected end of stream");

            do
            {
                sb.Append((char)r);
                r = Console.Read();
                b = IsBreak(r);
            } while (b == false);

            return sb.ToString();
        }

        bool? IsBreak(int c)
        {
            switch (c)
            {
                case -1:
                    return null;
                case (int)' ':
                case (int)'\n':
                case (int)'\r':
                    return true;
                default:
                    return false;
            }
        }

        public IEnumerable<int> Loop(int count) => Enumerable.Range(0, count);
        public IEnumerable<int> Loop(int from, int to) => Enumerable.Range(from, to - from + 1);

        public IEnumerable<long> Loop(long count) => Loop(0, count - 1);
        public IEnumerable<long> Loop(long from, long to)
        {
            for (long i = from; i <= to; i++)
                yield return i;
        }

        public string YesNo(bool isYes) => isYes ? "Yes" : "No";

        public HashSet<T> ToHashSet<T>(IEnumerable<T> ts) => new HashSet<T>(ts);



        struct Mint
        {
            public static long Divider { set { divider = value; } }
            private static long divider = 1000000007;
            public static void Set998244353() { divider = 998244353; }

            public long Value { get; }

            public Mint(long value)
            {
                this.Value = value;
            }

            public static Mint operator +(Mint a, Mint b) => new Mint((a.Value + b.Value) % divider);

            public static Mint operator -(Mint a, Mint b)
            {
                var t = (a.Value - b.Value) % divider;
                if (t < 0)
                    t += divider;
                return new Mint(t);
            }

            public static Mint operator *(Mint a, Mint b) => new Mint((a.Value * b.Value) % divider);

            public static Mint operator /(Mint a, Mint b) => new Mint((a.Value * InvImpl(b.Value)) % divider);

            public Mint Pow(long p) => new Mint(PowImpl(Value, p));
            private static long PowImpl(long a, long p)
            {
                if (p == 0)
                    return 1L;

                if (p == 1)
                    return a;

                var halfP = p / 2;
                var halfPowered = PowImpl(a, halfP);
                var powered = halfPowered * halfPowered % divider;
                return p % 2 == 0 ? powered : powered * a % divider;
            }

            public static Mint Inv(long a) => new Mint(InvImpl(a));
            private static readonly Dictionary<long, long> invCache = new Dictionary<long, long>();
            private static long InvImpl(long a)
            {
                long cache = 0L;
                if (invCache.TryGetValue(a, out cache))
                    return cache;

                var result = PowImpl(a, divider - 2);
                invCache.Add(a, result);
                return result;
            }

            public static Mint Fac(long a) => new Mint(FacImpl(a));
            private static readonly List<long> facCache = new List<long>() { 1L };
            private static long FacImpl(long a)
            {
                if (a >= divider)
                    return 0;

                if (a < facCache.Count)
                    return facCache[(int)a];

                var val = facCache.Last();
                var start = facCache.Count;
                for (int i = start; i <= a; i++)
                {
                    val = (val * i) % divider;
                    facCache.Add(val);
                }

                return val;
            }

            public static Mint Perm(long n, long r) => new Mint(PermImpl(n, r));
            private static long PermImpl(long n, long r)
            {
                if (n < r)
                    return 0L;

                if (r <= 0)
                    return 1L;

                var nn = FacImpl(n);
                var nr = FacImpl(n - r);
                return (nn * InvImpl(nr)) % divider;
            }

            public static Mint Comb(long n, long r) => new Mint(CombImpl(n, r));
            private static long CombImpl(long n, long r)
            {
                if (n < r)
                    return 0L;

                if (n == r)
                    return 1L;

                var nn = FacImpl(n);
                var nr = FacImpl(n - r);
                var rr = FacImpl(r);

                var nndnr = (nn * InvImpl(nr)) % divider;
                return (nndnr * InvImpl(rr)) % divider;
            }
        }

        struct RGB
        {
            public long r;
            public long g;
            public long b;

            public long Get(int i)
            {
                switch (i)
                {
                    case 0: return r;
                    case 1: return g;
                    case 2: return b;
                }

                throw new Exception("index error");
            }

            public RGB Next(int i)
            {
                switch (i)
                {
                    case 0: return new RGB() { r = r + 1, g = g, b = b };
                    case 1: return new RGB() { r = r, g = g + 1, b = b };
                    case 2: return new RGB() { r = r, g = g, b = b + 1 };
                }

                throw new Exception("index error");
            }
        }


        Mint Slv()
        {
            var n = ScanInt;
            var a = ScanInts(n);

            var dic = new Dictionary<RGB, Mint>() { { new RGB(), new Mint(1) } };

            foreach (var aa in a)
            {
                var next = new Dictionary<RGB, Mint>();

                foreach (var pair in dic)
                {
                    var rgb = pair.Key;
                    foreach (var i in Loop(3))
                    {

                        if (rgb.Get(i) == aa)
                        {
                            var nrgb = rgb.Next(i);

                            if (next.ContainsKey(nrgb))
                                next[nrgb] += pair.Value;
                            else
                                next.Add(nrgb, pair.Value);
                        }

                    }

                }

                dic = next;
            }

            return dic.Values.Aggregate(new Mint(0), (x, y) => x + y);
        }

        public IEnumerable<string> Solve()
        {
            var res = Slv();

            yield return res.Value.ToString();
        }
    }
}