local mod=1000000007
local n=io.read("n")
local r,g,b=0,0,0
local combination=1
for i=1,n do
    local counter=0
    local a=io.read("n")
    if r==a then
        counter=counter+1
    end
    if g==a then
        counter=counter+1
    end
    if b==a then
        counter=counter+1
    end
    combination=combination*counter%mod
    if r==a then
        r=r+1
    elseif g==a then
        g=g+1
    elseif b==a then
        b=b+1
    end
end
print(combination)