N = gets.chomp.to_i
str = gets.split
a = Array.new(N) 
for i in 0...N do
    a[i] = str[i].to_i
end
b = 0
c = 0
d = 0
answer = 1
for i in 0...N do
    if a[i] == b
        if a[i] == d
            answer *= 3
            answer %= 1000000007
        elsif a[i] == c
            answer *= 2
            answer %= 1000000007
        end
        b += 1
    elsif a[i] == c
        if a[i] == d
            answer *= 2
            answer %= 1000000007
        end
        c += 1
    elsif a[i] == d
        d += 1
    else 
        puts 0
        exit
    end
end
puts answer