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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;
use std::ops::{
    ShrAssign,
    Sub,
    Div,
    Mul,
    Rem,
    BitAnd,
};


fn main() {
    input! {
        s: String,
    }
    let mut tmp = 0;

    let mut mod_seq: Vec<usize> = vec![0];
    for (i, v) in s.chars().rev().enumerate() {
        let v = v.to_digit(10).unwrap() as usize;
        tmp = (v * modpow(10, i, 2019) + tmp) % 2019;
        mod_seq.push(tmp);
    }

    let mut counter = vec![0; 2019];
    for &c in mod_seq.iter() {
        counter[c as usize] += 1;
    }

    let mut ans = 0;
    for c in counter.iter() {
        ans += c * (c - 1) / 2;
    }

    println!("{}", ans);
}


pub fn modpow<T>(mut a: T, mut n: T, m: T) -> T
    where T: Sub<Output=T> + Div<Output=T> + Mul<Output=T> + Rem<Output=T> + ShrAssign + PartialOrd + BitAnd<Output=T> + Copy + From<u8>
{
    let zero = T::from(0);

    let one = T::from(1);
    let mut res = one;

    while n > zero {
        if (n & one) == one {
            res = res * a % m;
        }
        a = a * a % m;
        n >>= one;
    }

    res
}