N = gets.to_i
A = gets.split.map &:to_i

t = [ 0, 0, 0 ]

result = 1
for i in 0..N-1
  if t.count( A[i] ) == 0
    result = 0
    break
  end
  result = ( result * t.count( A[i] ) ) % 1000000007
  for j in 0..2
    if t[j] == A[i]
      t[j] += 1
      break
    end
  end
end
puts result