#include <iostream>
#include <vector>
using namespace std;
constexpr int MOD = 1000000007;

int main()
{
	int N; cin >> N;
	vector<int> A(N), B(N+1);
	for (int i = 0; i < N; ++i) cin >> A[i];
	B[0] = 3;
	long long ans = 1;
	for (int i = 0; i < N; ++i) {
		ans = ans * B[A[i]] % MOD;
		++B[-~A[i]];
		--B[A[i]];
	}
	cout << ans << endl;
	return 0;
}