MOD = 10 ** 9 + 7
N = gets.to_i
A = gets.split.map(&:to_i)
S = [0,0,0]
ans = A.inject(1) do |s, a|
  i = S.index(a)
  c = S.count(a)
  
  break 0 unless i
  
  S[i] += 1 
  (s * c) % MOD
end
puts ans
