N,*A = `dd`.split.map &:to_i
Mod = 1000000007
ans = 1
s = [0,0,0]
A.each{ |a|
  i = s.index(a)
  if i
    if a > 0
      m = s.count(a)
      ans = ans * m % Mod
    end
    s[i] += 1
    #p [m,ans,s]
  else
    p 0
	exit
  end
}

zs = s.count(0)
p ans * [6,3,3][zs] % Mod
