import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    n <- getInt
    as <- getIntList
    print . (\(x,_,_,_) -> x) $ foldl' solve (1,0,0,0) as

solve (s,a,b,c) d
    | d == c && a == c && b == c = (3*s `mod` 1000000007,a,b,c+1)
    | d == c && b == c = (2*s `mod` 1000000007,a,b,c+1)
    | d == c = (s,a,b,c+1)
    | d == b && a == b = (2*s `mod` 1000000007,a,b+1,c)
    | d == b = (s,a,b+1,c)
    | d == a = (s,a+1,b,c)