unittest
{
	assert( [ "6", "0 1 2 3 4 5" ].parse.expand.solve == 3 );
	assert( [ "3", "0 0 0" ].parse.expand.solve == 6 );
	assert( [ "54", "0 0 1 0 1 2 1 2 3 2 3 3 4 4 5 4 6 5 7 8 5 6 6 7 7 8 8 9 9 10 10 11 9 12 10 13 14 11 11 12 12 13 13 14 14 15 15 15 16 16 16 17 17 17" ].parse.expand.solve == 115295190 );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( as );
}

auto solve( long[] as )
{
	auto ans = 1L;
	auto cs = new long[ 3 ];
	foreach( a; as )
	{
		ans = ans.mulMod( cs.count( a ) );
		foreach( ref c; cs )
		{
			if( c == a )
			{
				c++;
				break;
			}
		}
	}
	return ans;
}

// 掛け算
auto mulMod( long MOD = 10L ^^ 9 + 7 )( long x, long y )
{
	return x * y % MOD;
}
