Problem:
Score : 400 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.

Find the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the following condition:

Condition: In base ten, the i-th through j-th characters of S form an integer that is a multiple of 2019.

Constraints

1 ≤ |S| ≤ 200000

S is a string consisting of digits from 1 through 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of pairs of integers (i,j) (1 ≤ i ≤ j ≤ |S|) that satisfy the condition.

Sample Input 1

1817181712114

Sample Output 1

3

Three pairs - (1,5), (5,9), and (9,13) - satisfy the condition.

Sample Input 2

14282668646

Sample Output 2

2

Sample Input 3

2119

Sample Output 3

0

No pairs satisfy the condition.

Solution in Haskell (GHC 8.8.3) (judge verdict: Time Limit Exceeded)

{-# 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