#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

int cnt[1000005];

int main() {
	int n;
	scanf("%d", &n);
	
	cnt[0] = 3;
	
	long long res = 1LL;
	
	for (int i = 0; i < n; i++) {
		int x;
		scanf("%d", &x);
		x++;
		
		res = (res * cnt[x - 1]) % MOD;
		cnt[x - 1]--;
		cnt[x]++;
	}
	
	printf("%lld\n", res);
}
