function Main(input) {
   	var inputArray = input.split("\n");
	var N=inputArray[0]*1;
	var InputArray2=inputArray[1].split(" ");
	
	var red=0;
	var green=0;
	var blue=0;
	
	var specific;
	var count=0;
	
	var answer=1;
	
	var Idx;
	
	for(var i=0;i<N;i++){
		count=0;
		Idx=0;
		specific=InputArray2[i]*1;
		if(specific==red){
			count++;
			red++;
			Idx=1
			
		}
		if(specific==green){
			count++;
			if(Idx==0){
				green++;
				Idx=1
			}
		}
		if(specific==blue){
			count++;
			if(Idx==0){
				blue++;
				Idx=1;
			}
		}
		
		//console.log(answer);
		answer=answer*count;
		
	}
	
	answer=answer%1000000007;
	console.log(answer);
	
}
     
Main(require("fs").readFileSync("/dev/stdin", "utf8"));

