n = gets.to_i
a = gets.split(' ').map(&:to_i)
cand = Array.new(n + 1, 0) # i = {i-1 | not chosen yet}
cand[0] = 3
mod = 1_000_000_007
res = 1
a.each do |x|
  res *= cand[x] # *=#{x-1 | not chosen yet}
  res %= mod
  cand[x] -= 1
  cand[x + 1] += 1
end
p res