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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOREACH(x, a) for(auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001; // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll t;
ll ret = 0;
ll table[ARRAY][3];
ll color = 0;

void input() {
  IN(N);
  ll A;
  REP(i, ARRAY) {
    REP(j, 3) {
      table[i][j] = -1;
    }
  }
  FOR(i, 1, N+1) {
    IN(A);
    if (table[A][0] == -1) {
      table[A][0] = i;
      CHMAX(color, 1);
    } else if (table[A][1] == -1) {
      table[A][1] = i;
      CHMAX(color, 2);
    } else {
      table[A][2] = i;
      CHMAX(color, 3);
    }
  }
  // REP(i, 20) {
  //   DEBUG(i);
  //   DEBUG("...");
  //   REP(j, 3) {
  //     DEBUG(table[i][j]);
  //   }
  //   LN();
  // }
}

ll align(ll i, ll j) {
  ll ans = 0;
  ll emp = 0;
  vector<ll> a;
  vector<ll> b;
  REP(k, 3) {
    if (table[i][k] != -1) {
      a.pb(table[i][k]);
      b.pb(table[j][k]);
      if (table[j][k] == -1) emp++;
    }
  }
  if (a.size() == 0 || b[0] == -1) {
    return 1;
  }
  ll n = a.size();
  do {
    bool flg = true;
    REP(i, n) {
      if (a[i] < b[i] || b[i] == -1) flg &= true;
      else flg &= false;
    }
    if (flg) {
      ans++;
    }
  } while(next_permutation(a.begin(), a.end()));
  // DEBUG(i);
  // DEBUG(" -> ");
  // DEBUG(j);
  // DEBUG(" = ");
  // DEBUG(ans);
  // LN();
  if (emp == 2) ans /= 2;
  return ans;
}

void solve() {
  ret = 1;
  REP(i, N) {
    ret *= align(i, i+1);
    ret %= MOD;
  }
  if (color == 1) {
    ret *= 3;
    ret %= MOD;
  } else if (color == 2) {
    ret *= 3;
    ret %= MOD;
  } else if (color == 3) {
    ret *= 6;
    ret %= MOD;
  }
  PRINT(ret);
}

int main(void){
  input();
  solve();
}