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,-1,-1,-1) as

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