n = gets.to_i
a = gets.split(' ').map(&:to_i)
cand = Array.new(n, 0)
mod = 1_000_000_007
res = 1
chainc = 0
a.each do |x|
  if x.zero?
    chainc += 1
    cand[0] += 1
    next
  end
  res *= cand[x - 1]
  res %= mod
  cand[x - 1] -= 1
  cand[x] += 1
end
res *= chainc == 1 ? 3 : 6
res %= mod
p res
