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: Time Limit Exceeded)

using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        var S = Console.ReadLine();
        Console.WriteLine(Calc(S));
    }

    static string Calc(string S)
    {
        long Q = (long)1e9 + 7;
        int N = S.Length;
        var k = Enumerable.Range(0, N)
            .Where(i => S[N - 1 - i] == '?');
        int x = Enumerable.Range(0, N)
            .Where(i => S[i] != '?')
            .Select(i => int.Parse(S[i].ToString()) * pow13(N - 1 - i))
            .Sum();
        if(!k.Any()) return x % 13 == 5 ? "1" : "0";
        var d = Enumerable.Range(0, 13)
            .Select(_ => (long)0)
            .ToArray();
        int j = k.First();
        foreach(int i in Enumerable.Range(0, 10))
        {
            d[(i * pow13(j)) % 13] = 1;
        }
        for (int i = 1; i < k.Count(); i++)
        {
            j = k.ElementAt(i);
            int pow = pow13(j);
            var newd = Enumerable.Range(0, 13)
                .Select(_ => (long)0)
                .ToArray();
            for (int key = 0; key < 13; key++)
            {
                for (int l = 0; l < 10; l++)
                {
                    int mod = (l * pow) % 13;
                    int idx = (key + mod) % 13;
                    newd[idx] = (newd[idx] + d[key]) % Q;
                }
            }
            d = newd;
        }
        int m = ((5 - x % 13) + 13) % 13;
        return (d[m] % Q).ToString();
    }

    static int pow13(int n)
    {
        if(n == 0) return 1;
        else if(n == 1) return 10;
        else
        {
            return Enumerable.Range(0, n)
                .Select(_ => 10)
                .Aggregate((x1, x2) => (x1 * x2) % 13);
        }
    }
}