#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#define REP(i,a) for (int i = 0;i < (a);++i)
#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 ALL(obj) (obj).begin(),(obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main(){
    ll n;
    cin >> n;
    vector<ll>a(n);
    vector<int>count(n,0);
    int zerocount;
    REP(i,n){
        cin >> a[i];
    }
    if(a[0]!=0){
        cout << 0 << endl;
        return 0;
    }

    ll answer=1;
    REP(i,n){
        if(a[i]==0){
            answer = (answer*(3-count[0]))%MOD;
        }
        else{
            if(count[a[i]]>=count[a[i]-1]){
                cout << 0 << endl;
                return 0;
            }
            answer = (answer*(count[a[i]-1]-count[a[i]]))%MOD;
        }
        count[a[i]]++;
        if(count[a[i]]>3){
            cout << 0 << endl;
            return 0;
        }
    }
    cout << answer << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);  
const ld EPS = 0.0000000001;
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define REPD(i, n) for(ll i=n-1; 0<=i; i--)
#define FOR(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define FORD(i, a, b) for(ll i=a; (ll)(b)<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))


int main(){
    int n;  cin >> n;
    map<int, int> a;
    a[0] = 3;
    ll res=1;
    REP(i, n){
        int x;  cin >> x;
        res *= a[x];
        res %= MOD;
        a[x] -= 1;
        a[x+1] += 1;
    }
    cout << res << endl;
}