#include <bits/stdc++.h>
#define int long long
using namespace std;
map<int, int> A;
int m = 1000000007;
int32_t main(){
	int n;
  cin >> n;
  A[0] = 3;
  int ans = 1;
  for(int i = 0; i < n; i++){
  	int v;
    cin >> v;
    ans = (ans * A[v]) % m;
    A[v]--;
    A[v+1]++;
  }
  cout << ans << endl;
}