#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#define int long long
#define mod 1000000007
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int n, c[123456], ans = 1;
signed main() {
	cin >> n;
	c[0] = 3;
	for0(i, n) {
		int k; cin >> k; k++;
		c[k]++;
		ans *= c[k - 1] - (c[k] - 1);
		ans %= mod;
	}
	cout << ans << endl;
}