#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int mod=1e9+7;
int n,x,a,b,c;
ll ans=1;
int main(){
	cin>>n;
	a=b=c=0;
	for(int i=1;i<=n;i++){
		cin>>x;
		ll tmp=0;
		if(x==a)tmp++;
		if(x==b)tmp++;
		if(x==c)tmp++;
		ans*=tmp;ans%=mod; 
		if(x==a)a++;
		else if(x==b)b++;
		else if(x==c)c++;
	}
	cout<<ans<<endl;
    return 0;
}