Problem:
Score : 400 points

Problem Statement

Given is a string S. Each character in S is either a digit (0, ..., 9) or ?.

Among the integers obtained by replacing each occurrence of ? with a digit, how many have a remainder of 5 when divided by 13? An integer may begin with 0.

Since the answer can be enormous, print the count modulo 10^9+7.

Constraints

S is a string consisting of digits (0, ..., 9) and ?.

1 \leq |S| \leq 10^5

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of integers satisfying the condition, modulo 10^9+7.

Sample Input 1

??2??5

Sample Output 1

768

For example, 482305, 002865, and 972665 satisfy the condition.

Sample Input 2

?44

Sample Output 2

1

Only 044 satisfies the condition.

Sample Input 3

7?4

Sample Output 3

0

We may not be able to produce an integer satisfying the condition.

Sample Input 4

?6?42???8??2??06243????9??3???7258??5??7???????774????4?1??17???9?5?70???76???

Sample Output 4

153716888

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

namespace DigitsParades
{
    class Program
    {
        static void Main(string[] args)
        {
            var S = Console.ReadLine();
            var mod = 1000000007;
            var exi = false;
            //一桁目から扱えるようにするため、反転させる
            var charS = S.ToCharArray();
            Array.Reverse(charS);

            var figsum = 0L;
            for (int i = 0; i < S.Length; i++)
            {

                if(charS[i] != '?')
                {
                    var num = int.Parse(charS[i].ToString());
                    //繰り返し二乗法により10のi乗を13で割った数値を保存する
                    var mnum = RepeatSquaring(10, i,13);
                    figsum += mnum*num;
                }
                else
                {
                    exi = true;
                }
            }
            //初期に数字が入ってる数の13で割った余り
            figsum %= 13;
            //目的値(？の部分を足していってこの数字に合わせるようにする)
            long aim = 13 + 5;
            aim -= figsum;
            aim %= 13;
           // Console.WriteLine(aim);

            //それぞれのmodの組み合わせパターン
            var comb = new long[13];
            //var ans = 0L;
            var once = true;
            for (int i = 0; i < charS.Length; i++)
            {
                if (charS[i] == '?')
                {
                    //繰り返し二乗法により10のi乗を13で割った数値を保存する
                    var mnum = RepeatSquaring(10, i, 13);
                    var tmp = new long[13];
                    //?が0から9まで動くことを考慮する
                    for (int j = 1; j <= 9; j++)
                    {
                        //jとの和がaimと13で合同な数を探す。
                        //var comp = (13 + aim - figmod) % 13;
                        var figmod = (mnum * j) % 13;
                        if (once)
                        {
                            tmp[figmod]++;
                        }
                        for (int k = 0; k < 13; k++)
                        {
                            var tai = (figmod + k) % 13;
                            tmp[tai] += comb[k];
                            tmp[tai] %= mod;
                        }
                    }
                    if (once)
                    {
                        once = false;
                        comb[0]++;
                    }

                    //移す
                    for (int j = 0; j < 13; j++)
                    {
                        comb[j] += tmp[j];
                        comb[j] %= mod; 
                    }
                    
                }
            }

            if (exi)
            {
                Console.WriteLine(comb[aim]);
            }
            else if(figsum == 5)
            {
                Console.WriteLine(1);
            }
            else{
                Console.WriteLine(0);
            }
            
        }

        //繰り返し2乗法
        static long RepeatSquaring(long N, long M = 1000000005, long mod = 1000000007)
        {
            if (M == 0)
            {
                return 1;
            }
            else if (M % 2 == 0)
            {
                long t = RepeatSquaring(N, (M / 2), mod) % mod;
                return (t * t) % mod;
            }
            else
            {
                return ((N % mod) * RepeatSquaring(N, M - 1, mod)) % mod;
            }
        }
    }
}