#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> P;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

ll mod = 1000000007;
int main(){
  int n; cin >> n;
  vector <ll> a(n);
  rep(i, n){
    cin >> a[i];
    a[i]++;
  }
  vector <ll> num(n+1);
  num[0] = 3;
  ll ans = 1;
  rep(i, n){
    if (num[a[i]-1] < 0){
      cout << 0 << endl;
      return 0;
    }
    ans *= num[a[i]-1];
    ans %= mod;
    num[a[i]-1]--;
    num[a[i]]++;
  }
  cout << ans <<endl;
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
static const int MAX = 510000;
static const ll MOD = 1000000007;

int main(){
    int N;
    cin>>N;
    vector<ll> A(N);
    for(int i=0; i<N; i++) cin>>A[i];
    vector<ll> color(3,0);

    ll ans = 1;

    for(int i=0; i<N; i++){
        ll kake = 0;
        for(int j=0; j<3; j++){
            if(color[j] == A[i]) kake++;
        }

        ans *= kake;
        ans %= MOD;

        for(int j=0; j<3; j++){
            if(color[j] == A[i]){
                color[j]++;
                break;
            }
        }
    }

    cout<<ans<<endl;
}