import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  (n : _) <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  as <- map unsafeSignedDecimal . T.words <$> T.getLine :: IO [Int]
  print . fst . foldl step (1, (0, 0, 0)) $ as

step :: (Int, (Int, Int, Int)) -> Int -> (Int, (Int, Int, Int))
step (k, cs@(c0, c1, c2)) a = ((k * (length . filter (== True) $ [b0, b1, b2])) `mod` modulus, cs')
  where
    b0 = c0 == a
    b1 = c1 == a
    b2 = c2 == a
    cs' | b0 = (succ c0, c1, c2)
        | b1 = (c0, succ c1, c2)
        | b2 = (c0, c1, succ c2)
        | otherwise = cs

modulus = 1000000007 :: Int

unsafeSignedDecimal :: T.Text -> Int
unsafeSignedDecimal s = case T.signed T.decimal s of
  Right (n, _) -> n
