n=gets.to_i
a=gets.split.map(&:to_i)
l=[0]*100000
c=1
a.each do |b|
  if b==0
    if l[b]==0
      c*=3
    elsif l[b]==1
      c*=2
    elsif l[b]>2
      puts 0
      exit
    end
  else
    if l[b-1]-l[b]<=0
      puts 0
      exit
    end
    c*=l[b-1]-l[b]
  end
  l[b]+=1
  c%=1000000007
end
puts c
