import std.stdio;
import std.conv;
import std.string;
import std.range;

void main() {
	int n;
	scanf("%d\n", &n);
	auto as = readln.chomp.split.to!(int[]);
	int[3][] lad = new int[3][n];
	lad[0][0] = 1;
	long acc = 1;
	foreach(i; 1..n) {
		long cnt = 0;
		size_t idx;
		foreach(j; 3.iota.retro) {
			lad[i][j] = lad[i-1][j];
			if (as[i] == lad[i][j]) {
				++cnt;
				idx = j;
			}
		}
		acc = acc * cnt % 1000000007;
		++lad[i][idx];
	}
	write(acc * 3);
}
