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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            input_iter = LazyLoadAllLines();
            //------------------------------------------------//
            //var x = Read<long>();
            //var m = Read<long>();
            //var x = Read<long>();
            var n = Read<long>();
            // var s = Read();
            /*List<string> sls = new List<string>();
            for(int i = 0; i < h; ++i)
            {
                sls.Add(Read());
            }*/
            var als = Read<long>(n);
            //var fls = Read<long>(n);
            //List<long> als = new List<long>(), bls = new List<long>();
            /*List<List<long>> dcls = new List<List<long>>();
            for (int i = 0; i < m; ++i)
            {
                dcls.Add(Read<long>(2));
            }*/
            //List<List<long>> abls = new List<List<long>>();
            //List<long> cls = new List<long>();
            /*List<long> edgeNum = Enumerable.Repeat(0L, (int)n).ToList();
            for (int i = 0; i < n - 1; ++i)
            {
                abls.Add(Read<long>(2));
                abls[i].Add(i);
                abls[i].Add(-1);
                --abls[i][0];
                --abls[i][1];
                //cls.Add(Read<long>());
                ++edgeNum[(int)abls[i][0]];
                ++edgeNum[(int)abls[i][1]];
            }*/

            //als.Sort((l, r) => (int)(l - r));

            LongMod res = new LongMod(1);
            long[] cnts = new long[] { 0, 0, 0 };
            for(int i = 0; i < n; ++i)
            {
                var pattern = 0;
                var idx = -1;
                for(int j = 0; j < 3; ++j)
                {
                    if(cnts[j] == als[i])
                    {
                        ++pattern;
                        if (idx == -1) idx = j;
                    }
                }
                res *= pattern;
                ++cnts[idx];
            }

            Console.WriteLine(res.value);
        }

        public static void Swap<_T>(ref _T l, ref _T r)
        {
            _T temp = r;
            r = l;
            l = temp;
        }

        public class LongMod
        {
            public long value { get; private set; }
            public long mod { get; private set; }

            public LongMod(long v, long m = 1000000007L) { value = v; mod = m; }

            public static implicit operator long(LongMod v) { return v.value; }

            public static bool operator ==(LongMod l, LongMod r) { return (l.value == r.value && l.mod == r.mod); }
            public static bool operator !=(LongMod l, LongMod r) { return !(l == r); }
            public static bool operator >(LongMod l, LongMod r) { return (l.value > r.value && l.mod == r.mod); }
            public static bool operator <(LongMod l, LongMod r) { return !(l > r) && !(l == r); }
            public static bool operator >=(LongMod l, LongMod r) { return !(l < r); }
            public static bool operator <=(LongMod l, LongMod r) { return !(l > r); }

            public static LongMod operator +(LongMod l, LongMod r) { return new LongMod((l.value + r.value) % l.mod, l.mod); }
            public static LongMod operator +(LongMod l, long r) { return new LongMod((l.value + r) % l.mod, l.mod); }
            public static LongMod operator -(LongMod l, LongMod r) { return new LongMod((l.value + l.mod - r.value) % l.mod, l.mod); }
            public static LongMod operator -(LongMod l, long r) { return new LongMod((l.value + l.mod - r) % l.mod, l.mod); }
            public static LongMod operator *(LongMod l, LongMod r) { return new LongMod(((l.value % l.mod) * (r.value % l.mod)) % l.mod, l.mod); }
            public static LongMod operator *(LongMod l, long r) { return new LongMod(((l.value % l.mod) * (r % l.mod)) % l.mod, l.mod); }
            public static LongMod operator /(LongMod l, LongMod r) { return new LongMod(l * r.Invert()); }
            public static LongMod operator /(LongMod l, long r) { return new LongMod(l * new LongMod(r, l.mod).Invert()); }
            public static LongMod operator %(LongMod l, LongMod r) { return new LongMod(l.value % r.value, r.value); }
            public static LongMod operator %(LongMod l, long r) { return new LongMod(l.value % r, r); }

            public LongMod Invert()
            {
                // 逆元 (1/x) : 存在条件 = valueとmodが互いに素
                // memo : mod = 10^9+7(素数)の時, value != 0ならok.
                long a = value, b = mod, u = 1, v = 0;
                while (b > 0)
                {
                    long t = a / b;
                    a -= t * b; Swap(ref a, ref b);
                    u -= t * v; Swap(ref u, ref v);
                }
                u %= mod;
                if (u < 0) u += mod;
                return new LongMod(u, mod);
            }

            public LongMod Power(long exp)
            {
                // べき乗
                long a = value, res = 1;
                while (exp > 0)
                {
                    if ((exp & 1) > 0) res = res * a % mod;
                    a = a * a % mod;
                    exp >>= 1;
                }
                return new LongMod(res, mod);
            }

            public LongMod Factorial()
            {
                // 階乗
                LongMod res = new LongMod(1L, mod);
                for (long i = value; i > 0; --i) res *= i;
                return res;
            }
        }

        static IEnumerator<string> input_iter;

        static _T Read<_T>() where _T : struct
        {
            input_iter.MoveNext();
            return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
        }
        static string Read()
        {
            input_iter.MoveNext();
            return input_iter.Current;
        }
        static List<_T> Read<_T>(long num) where _T : struct { return new List<_T>(Enumerate<_T>(num)); }


        static IEnumerable<_T> Enumerate<_T>(long num)
        {
            for (long i = 0; i < num; ++i)
            {
                input_iter.MoveNext();
                yield return (_T)Convert.ChangeType(input_iter.Current, typeof(_T));
            }
        }
        static IEnumerator<string> LazyLoadAllLines()
        {
            string line;
            do
            {
                line = Console.ReadLine();
                foreach (var s in line.Split(' '))
                    yield return s;
            } while (line != null);
        }

        public class PriorityQueue<T> where T : IComparable
        {
            private IComparer<T> comparer = null;
            private bool isAccendingOrder;

            private List<T> heap;
            private int heapCurrentSize = 0;

            public PriorityQueue(IComparer<T> comparer, int initialHeapSize = 256)
            {
                heap = new List<T>(initialHeapSize);
                this.comparer = comparer;
            }

            public PriorityQueue(bool isAccending = true, int initialHeapSize = 256)
            {
                heap = new List<T>(initialHeapSize);
                isAccendingOrder = isAccending;
            }

            private int Compare(T x, T y)
            {
                if (comparer != null) return comparer.Compare(x, y);
                return isAccendingOrder ? x.CompareTo(y) : y.CompareTo(x);
            }

            public bool Empty()
            {
                return Count() == 0;
            }

            public int Count()
            {
                return heapCurrentSize;
            }

            public void Push(T x)
            {
                //node number
                var idx = heapCurrentSize++;
                if (heapCurrentSize > heap.Count) heap.Capacity *= 2;

                while (idx > 0)
                {
                    //parent node number
                    var parentIdx = (idx - 1) / 2;

                    if (Compare(heap[parentIdx], x) <= 0) break;

                    heap[idx] = heap[parentIdx];
                    idx = parentIdx;
                }

                heap[idx] = x;
            }

            public T Pop()
            {
                T ret = heap[0];
                T x = heap[--heapCurrentSize];

                int idx = 0;
                while (idx * 2 + 1 < heapCurrentSize)
                {
                    //children
                    int a = idx * 2 + 1;
                    int b = idx * 2 + 2;

                    if (b < heapCurrentSize && Compare(heap[b], heap[a]) < 0) a = b;

                    if (Compare(heap[a], x) >= 0) break;

                    heap[idx] = heap[a];
                    idx = a;
                }

                heap[idx] = x;

                return ret;
            }

            public T Peek()
            {
                return heap[0];
            }

            public bool Contains(T x)
            {
                for (int i = 0; i < heapCurrentSize; i++) if (x.Equals(heap[i])) return true;
                return false;
            }

            public void Clear()
            {
                while (heapCurrentSize > 0) Pop();
            }

            public IEnumerator<T> GetEnumerator()
            {
                var temp = new List<T>();

                while (heapCurrentSize > 0)
                {
                    temp.Add(Pop());
                }

                foreach (var value in temp)
                {
                    Push(value);
                    yield return value;
                }
            }

            public T[] ToArray()
            {
                T[] array = new T[heapCurrentSize];
                int i = 0;

                foreach (var r in this)
                {
                    array[i] = r;
                    ++i;
                }

                return array;
            }
        }
    }
}