n = gets.to_i
a = gets.split.map(&:to_i)
ans = 1
cs = [0, 0, 0]
a.each do |c|
    match = cs.count(c)
    ans *= match % 1000000007
    cs.each_with_index do |k, i|
        if k == c
            cs[i] += 1
            break
        end
    end
end
puts ans % 1000000007