#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define COUT(x) cout<<(x)<<"\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define REP(i, n) for(int i=0;i<n;i++)
#define YES(x) cout<<(x?"YES":"NO")<<"\n"
#define Yes(x) cout<<(x?"Yes":"No")<<"\n"
#define dump(x) cout<<#x<<" = "<<(x)<<"\n"
#define endl "\n"
using G = vector<vector<int>>;
using M = map<int,int>;
using P = pair<int,int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int,vector<int>,greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct { int to; int cost; };
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

int main() {
  IOS;
  int n; cin >> n;
  vector<int> a(n);
  REP(i,n) cin >> a[i];
  vector<int> c(n+1);
  c[0] = 3;
  ll ans = 1;
  REP(i,n) {
    ans*=c[a[i]];
    ans%=MOD;
    c[a[i]]--;
    c[a[i]+1]++;
  }
  COUT(ans);
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007

int main() {
    int n;
    long long int ans = 3;
    cin >> n;
    int a[n], rgb[3] = {0};
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }
  	if(a[0] != 0) {
    	cout << 0 << '\n';
      	return 0;
    }
    rgb[0] = 1;
    for(int i = 1; i < n; ++i) {
        int sum = 0;
        for(int j = 0; j < 3; ++j) {
            if(rgb[j] == a[i]) sum++;
        }
        ans *= sum;
        ans %= MOD;
        for(int j = 0; j < 3; ++j) {
            if(rgb[j] == a[i]) {
                rgb[j]++;
                break;
            }
        }
    }
    cout << ans << '\n';

    return 0;
}
