MOD = 10 ** 9 + 7
N = gets.to_i
A = gets.split.map(&:to_i)

counter = Array.new(3, 0)
patterns = []

A.each do |a|
  patterns << counter.count { |c| c == a }

  3.times do |i|
    if counter[i] == a
      counter[i] += 1
      break
    end
  end
end

puts patterns.inject(:*) % MOD
