n = gets.to_i
a = gets.split.map(&:to_i)
x = [0, 0, 0]
ans = 1
n.times do |i|
  mul = 0
  3.times do |j|
    mul += 1 if x[j] == a[i]
  end
  3.times do |j|
    if x[j] == a[i] then
      x[j] += 1
      break
    end
  end
  ans *= mul
  ans %= 1000000007 if ans > 1000000007
end
puts ans