#include <bits/stdc++.h>
using namespace std;

int main()
{
	int64_t n, mod = 1000000007;
	cin >> n;
	vector<int64_t> as(n);
	for (auto &&a : as)
	{
		cin >> a;
	}

	int64_t ans = 1;

	vector<int64_t> cnt(3, 0);
	for (auto &&a : as)
	{
		int64_t x = 0;
		for (auto &&c : cnt)
		{
			if (c == a)
			{
				x++;
			}
		}
		for (auto &&c : cnt)
		{
			if (c == a)
			{
				c++;
				break;
			}
		}
		(ans *= x) %= mod;
	}

	cout << ans << endl;

	return 0;
}