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 (Clang 3.8.0) (judge verdict: Accepted)

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>

#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))

int compare_sz_asc(const void* a, const void* b) {
    return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}

int compare_sz_desc(const void* a, const void* b) {
    return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}

int compare_i64_asc(const void* a, const void* b) {
    return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}

int compare_i64_desc(const void* a, const void* b) {
    return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}

int compare_c_asc(const void* a, const void* b) {
    return *((char*)a) < *((char*)b) ? -1 : 1;
}

int compare_c_desc(const void* a, const void* b) {
    return *((char*)a) > * ((char*)b) ? -1 : 1;
}

static size_t powSz(const size_t base, const size_t exp) {
    if (exp == 0) {
        return 1;
    }

    if (exp == 1) {
        return base;
    }

    if (exp % 2 == 0) {
        return powSz(base * base, exp / 2);
    }
    else {
        return base * powSz(base, exp - 1);
    }
}

static size_t comb(const size_t n, const size_t r) {
    size_t result = 1;

    for (size_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t combU64(const uint64_t n, const uint64_t r) {
    uint64_t result = 1;

    for (uint64_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static size_t gcdZu(size_t m, size_t n) {
    size_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

static uint64_t gcdU64(uint64_t m, uint64_t n)
{
    uint64_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

static char S[100001];
static uint64_t dp[100000][13];

#define MOD_VAL (1000000007)

int main(void) {
    scanf("%s", S);

    const size_t len = strlen(S);

    for (size_t i = 0; i < 13; i++) {
        dp[len - 1][i] = 0;
    }
    if (S[len - 1] == '?') {
        for (size_t i = 0; i < 13; i++) {
            if (i < 10) {
                dp[len - 1][i] = 1;
            }
        }
    }
    else {
        dp[len - 1][S[len - 1] - '0'] = 1;
    }

    uint64_t baseMod = 1;

    for (size_t ofs = 1; ofs < len; ofs++) {
        const size_t idx = len - ofs - 1;
        for (size_t i = 0; i < 13; i++) {
            dp[idx][i] = 0;
        }
        if (S[idx] == '?') {
            for (uint64_t digVal = 0; digVal < 10; digVal++) {
                if (digVal == 0) {
                    for (uint64_t i = 0; i < 13; i++) {
                        dp[idx][i] = (dp[idx][i] + dp[idx + 1][i]) % MOD_VAL;
                    }
                }
                else {
                    for (uint64_t i = 0; i < 13; i++) {
                        const uint64_t modVal = (digVal * 10 * baseMod + i) % 13;
                        dp[idx][modVal] = (dp[idx][modVal] + dp[idx + 1][i]) % MOD_VAL;
                    }
                }
            }
        }
        else {
            if (S[idx] == '0') {
                for (uint64_t i = 0; i < 13; i++) {
                    dp[idx][i] = dp[idx + 1][i];
                }
            }
            else {
                for (uint64_t i = 0; i < 13; i++) {
                    const uint64_t modVal = ((uint64_t)(S[idx] - '0') * 10 * baseMod + i) % 13;
                    dp[idx][modVal] = (dp[idx][modVal] + dp[idx + 1][i]) % MOD_VAL;
                }
            }
        }

        baseMod = baseMod * 10 % 13;
    }

    printf("%"PRIu64, dp[0][5]);

    return 0;
}