#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

const long long MOD = 1000000007;
long long N;
long long a[100000] = {0};

int main() {
  cin >> N;
  long long ans = 1;
  rep(i, N) {
    long long A;
    cin >> A;
    long long temp;
    if(A == 0) {
      temp = 3 - a[A];
    } else {
      temp = a[A-1] - a[A];
    }
    ans =  ans * temp % MOD;
    a[A]++;
  }
  if(ans <= 0) cout << 0 << endl;
  else cout << ans << endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
 
using ll = long long;

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;


int main(){
   int n;
   cin >> n;
   vector<ll>v(100000);
   vector<int>a(n);
   rep(i, 0, n)cin >> a[i];
   ll ans = 1;
   rep(i, 0, n){
       ll p = a[i];
       if(p==0){
           v[p]++;
           ans *= (4-v[p]);
           ans %= mod;
       }else{
           v[p]++;
           ans *= min(v[p-1],v[p-1]+1-v[p]);
           ans %= mod;
       }
       //cout << ans << endl;
   }
   cout << ans << endl;
}