{-# LANGUAGE BangPatterns #-}

import qualified Data.Vector.Unboxed as U
-- import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import Data.List
-- import Control.Monad
import Control.Arrow
--import Data.Foldable
--import Data.Char
import qualified Data.IntMap.Strict as IM

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine




main = do
    s <- U.map (read.pure) <$> U.fromList <$> getLine :: IO (U.Vector Int)
    let lscan = U.imap (\i a -> (i,a`mod`3) ) $ U.scanl (+) 0 s
    let zero = U.map fst $ U.filter (\(i,a)->a==0) lscan
    let one = U.map fst $ U.filter (\(i,a)->a==1) lscan
    let two = U.map fst $ U.filter (\(i,a)->a==2) lscan
    print $ solve zero s
    {-print $-} + solve one s
    {-print $-} + solve two s

solve xs s = naive (U.toList xs) s

naive [] s = 0
naive (x:xs) s = length (filter (\(i,j) -> (==0) $ (`mod`673) $ read $ map (head.show) $ U.toList (U.slice i j s)) [(x,y-x) | y<-xs])
                    + naive xs s



