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: Wrong Answer)

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

class Program
{
    static void Main(string[] args)
    {
        var S = sc.ReadStr();

        var dp = new long[13];
        dp[0] = 1;
        long k = 1;
        for (int i = S.Length - 1; i >= 0; i--)
        {
            var nextDP = new long[13];
            if (S[i] == '?')
            {
                for (int x = 0; x <= 9; x++)
                {
                    var d = x * k;
                    for (int j = 0; j < dp.Length; j++)
                    {
                        nextDP[(d + j) % 13] += dp[j];
                        nextDP[(d + j) % 13] %= MOD;
                    }
                }
            }
            else
            {
                var d = ((int)S[i] - '0') * k;
                for (int j = 0; j < dp.Length; j++)
                {
                    nextDP[(d + j) % 13] += dp[j];
                    nextDP[(d + j) % 13] %= MOD;
                }
            }
            dp = nextDP;
            k *= 10;
            k %= MOD;
        }
        var ans = dp[5];
        Console.WriteLine(ans);
    }

    static int MOD = (int)1e9 + 7;
    static int INF = (int)1e7 + 1;
    static long LINF = (long)1e13 + 1;
    static Random Rand = new Random();
    static Scanner sc = new Scanner();

    static long Gcd(long a, long b)
    {
        var v = new[] { a, b };
        while (v[1] != 0) { v = new[] { v[1], v[0] % v[1] }; }
        return v[0];
    }
    static long Lcm(long a, long b)
    {
        return a / Gcd(a, b) * b;
    }
    static bool IsPrime(long num)
    {
        if (num < 2) return false;
        else if (num == 2) return true;
        else if (num % 2 == 0) return false;

        double sqrtNum = Math.Sqrt(num);
        for (int i = 3; i <= sqrtNum; i += 2)
        {
            if (num % i == 0)
            {
                return false;
            }
        }
        return true;
    }
    static List<long> PrimeFactors(long n)
    {
        var res = new List<long>();
        var tmp = n;
        for (long x = 2; x * x <= n; x++)
        {
            while (tmp % x == 0)
            {
                tmp /= x;
                res.Add(x);
            }
        }
        if (tmp != 1) res.Add(tmp);
        return res;
    }
    static long[] Divisor(long n)
    {
        var res = new List<long>();
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                res.Add(i);
                if (i * i != n) res.Add(n / i);
            }
        }
        return res.ToArray();
    }
    static Result[] ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f)
    {
        Result[] res = new Result[a.Count() * b.Count()];
        var i = 0;
        foreach (var v in a)
            foreach (var w in b)
                res[i++] = f(v, w);
        return res;
    }
    static IEnumerable<Result> ListAll<T1, T2, Result>(IEnumerable<T1> a, IEnumerable<T2> b, Func<T1, T2, Result> f, Func<T1, T2, bool> s)
    {
        foreach (var v in a)
        {
            foreach (var w in b)
                if (s(v, w)) yield return f(v, w);
        }
    }
    static List<long> Facs = new List<long>() { 1 };
    static long Fac(int n)
    {
        for (int i = Facs.Count; i <= n; i++)
        {
            Facs.Add(i * Facs[i - 1]);
        }
        return Facs[n];
    }
    static long nCr(int n, int r)
    {
        if (n < r) return 0;
        if (n == r) return 1;
        var res = Fac(n);
        res /= Fac(r);
        res /= Fac(n - r);
        return res;
    }
}

public struct ModInt
{
    public const long Mod = (int)1e9 + 7;
    public long num;
    public ModInt(long n) { num = n; }
    public override string ToString() { return num.ToString(); }
    public static ModInt operator +(ModInt l, ModInt r) { l.num += r.num; if (l.num >= Mod) l.num -= Mod; return l; }
    public static ModInt operator -(ModInt l, ModInt r) { l.num -= r.num; if (l.num < 0) l.num += Mod; return l; }
    public static ModInt operator *(ModInt l, ModInt r) { return new ModInt(l.num * r.num % Mod); }
    public static implicit operator ModInt(long n) { n %= Mod; if (n < 0) n += Mod; return new ModInt(n); }
    public static ModInt Pow(ModInt v, long k) { return Pow(v.num, k); }
    public static ModInt Pow(long v, long k)
    {
        long ret = 1;
        for (k %= Mod - 1; k > 0; k >>= 1, v = v * v % Mod)
            if ((k & 1) == 1) ret = ret * v % Mod;
        return new ModInt(ret);
    }
    public static ModInt Inverse(ModInt v) { return Pow(v, Mod - 2); }
}
public class BinomialCoefficient
{
    public ModInt[] fact, ifact;
    public BinomialCoefficient(int n)
    {
        fact = new ModInt[n + 1];
        ifact = new ModInt[n + 1];
        fact[0] = 1;
        for (int i = 1; i <= n; i++)
            fact[i] = fact[i - 1] * i;
        ifact[n] = ModInt.Inverse(fact[n]);
        for (int i = n - 1; i >= 0; i--)
            ifact[i] = ifact[i + 1] * (i + 1);
        ifact[0] = ifact[1];
    }
    public ModInt this[int n, int r]
    {
        get
        {
            if (n < 0 || n >= fact.Length || r < 0 || r > n) return 0;
            return fact[n] * ifact[n - r] * ifact[r];
        }
    }
    public ModInt RepeatedCombination(int n, int k)
    {
        if (k == 0) return 1;
        return this[n + k - 1, k];
    }
}

class Debug
{
    public static void Dump<T>(T[,] array)
    {
        Console.WriteLine();
        var h = array.GetLength(0);
        var w = array.GetLength(1);

        for (int r = 0; r < h; r++)
        {
            for (int c = 0; c < w; c++)
            {
                Console.Write(array[r, c]);
                Console.Write(" ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
    }
    public static void Dump<T>(T[] array)
    {
        Console.WriteLine();
        Console.WriteLine(string.Join(" ", array));
        Console.WriteLine();
    }
}

class Scanner
{
    string[] S = new string[0];
    int Index = 0;
    char[] Separators = new char[] { ' ' };

    public string Next()
    {
        if (this.Index < this.S.Length) return this.S[this.Index++];
        var line = "";
        while (line == "") line = Console.ReadLine();
        this.S = line.Split(this.Separators, StringSplitOptions.RemoveEmptyEntries);
        if (this.S.Length == 0) return this.Next();
        this.Index = 0;
        return this.S[this.Index++];
    }

    public string ReadStr()
    {
        return this.Next();
    }

    public char ReadChar()
    {
        return this.Next()[0];
    }

    public int ReadInt()
    {
        return int.Parse(this.Next());
    }

    public uint ReadUInt()
    {
        return uint.Parse(this.Next());
    }

    public long ReadLong()
    {
        return long.Parse(this.Next());
    }

    public int[] ReadIntArray(int n)
    {
        var array = new int[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadInt();
        }
        return array;
    }

    public long[] ReadLongArray(int n)
    {
        var array = new long[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadLong();
        }
        return array;
    }

    public char[] ReadCharArray(int n)
    {
        var array = new char[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadChar();
        }
        return array;
    }

    public string[] ReadStrArray(int n)
    {
        var array = new string[n];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = this.ReadStr();
        }
        return array;
    }
}

static class Permutation<T>
{
    private static void Search(List<T[]> perms, Stack<T> stack, T[] a)
    {
        int N = a.Length;
        if (N == 0)
        {
            perms.Add(stack.Reverse().ToArray());
        }
        else
        {
            var b = new T[N - 1];
            Array.Copy(a, 1, b, 0, N - 1);
            for (int i = 0; i < a.Length; ++i)
            {
                stack.Push(a[i]);
                Search(perms, stack, b);
                if (i < b.Length) { b[i] = a[i]; }
                stack.Pop();
            }
        }
    }
    public static List<T[]> All(params T[] source)
    {
        var perms = new List<T[]>();
        Search(perms, new Stack<T>(), source);
        return perms;
    }
}

class HashMap<K, V> : Dictionary<K, V> where V : new()
{
    private V DefaltValue = new V();
    public HashMap() { }
    public HashMap(V defaultValue)
    {
        this.DefaltValue = new V();
    }
    new public V this[K i]
    {
        get
        {
            V v;
            return TryGetValue(i, out v) ? v : base[i] = this.DefaltValue;
        }
        set { base[i] = value; }
    }
}

class Vector<T> : List<T> where T : new()
{
    public Vector() { }
    public Vector(int count, T defaultValue)
    {
        for (int i = 0; i < count; i++)
        {
            this.Add(defaultValue);
        }
    }
}

class PriorityQueue<T> where T : IComparable<T>
{
    public readonly T[] Array;
    public readonly bool Greater;
    public int Count { get; private set; } = 0;

    public PriorityQueue(int capacity, bool greater = true)
    {
        this.Array = new T[capacity];
        this.Greater = greater;
    }

    public void Enqueue(T item)
    {
        this.Array[this.Count] = item;
        this.Count += 1;
        this.UpHeap();
    }

    public T Dequeue()
    {
        this.Swap(0, this.Count - 1);
        this.Count -= 1;
        this.DownHeap();

        return this.Array[this.Count];
    }

    public T Peek()
    {
        return this.Array[0];
    }

    private void UpHeap()
    {
        var n = this.Count - 1;
        while (n != 0)
        {
            var parent = (n - 1) / 2;

            if (this.Array[n].CompareTo(this.Array[parent]) > 0)
            {
                this.Swap(n, parent);
                n = parent;
            }
            else
            {
                break;
            }
        }
    }

    private void DownHeap()
    {
        var parent = 0;
        while (true)
        {
            var child = 2 * parent + 1;
            if (child > this.Count - 1) break;

            if (child < this.Count - 1 && this.Compare(this.Array[child], this.Array[child + 1]) < 0)
            {
                child += 1;
            }

            if (this.Compare(this.Array[parent], this.Array[child]) < 0)
            {
                this.Swap(parent, child);
                parent = child;
            }
            else
            {
                break;
            }
        }
    }

    private int Compare(T a, T b)
    {
        var c = a.CompareTo(b);
        if (!this.Greater)
        {
            c = -c;
        }
        return c;
    }

    private void Swap(int a, int b)
    {
        var tmp = this.Array[a];
        this.Array[a] = this.Array[b];
        this.Array[b] = tmp;
    }
}

class UnionFind
{
    public int[] Parents { get; set; }
    public UnionFind(int n)
    {
        this.Parents = new int[n];
        for (int i = 0; i < n; i++)
        {
            this.Parents[i] = i;
        }
    }
    public int Find(int x)
    {
        if (this.Parents[x] != x)
        {
            this.Parents[x] = Find(this.Parents[x]);
        }
        return this.Parents[x];
    }
    public bool Same(int x, int y)
    {
        return this.Find(x) == this.Find(y);
    }
    public int Union(int x, int y)
    {
        var xroot = this.Find(x);
        var yroot = this.Find(y);
        if (xroot != yroot) this.Parents[xroot] = yroot;
        return yroot;
    }
}
static class Algorithm
{
    static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb)
    {
        var l = 0;
        var r = a.Count - 1;
        while (l <= r)
        {
            var m = (l + r) / 2;
            var res = cmp.Compare(a[m], v);
            if (res < 0 || (res == 0 && !islb)) l = m + 1;
            else r = m - 1;
        }
        return l;
    }
    public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); }
    public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); }
    public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); }
    public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); }
}