n = gets.to_i
a = gets.split.map &:to_i

mod = 1000000007

arr = Array.new(n + 1, 0)
sum = 1

arr[0] = 3

a.each{ |aa|
	aa2 = aa + 1
	sum = (sum * (arr[aa2 - 1] - arr[aa2])) % mod
	arr[aa2] += 1
}

puts sum
