N = gets.to_i
A = gets.chomp.split(' ').map(&:to_i)

def count(colors, i)
  return 1 if i == N
  sum = 0
  (0..2).each do |j|
    if colors[j] == A[i]
      dup_colors = colors.dup
      dup_colors[j] += 1
      sum = (sum + count(dup_colors, i+1)) % 1000000007
    end
  end
  sum
end

puts count([0, 0, 0], 0)
