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 Go (1.14.1) (judge verdict: Runtime Error)

package main

import (
	"bufio"
	"flag"
	"fmt"
	"log"
	"os"
	"runtime"
	"runtime/pprof"
	"strconv"
)

func newScanner() *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	return scanner
}

var sc = newScanner()

func scanInt() int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func scanString() string {
	if sc.Scan() {
		return sc.Text()
	}
	panic(sc.Err())
}

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")

func main() {
	flag.Parse()
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal("could not create CPU profile: ", err)
		}
		defer f.Close() // error handling omitted for example
		if err := pprof.StartCPUProfile(f); err != nil {
			log.Fatal("could not start CPU profile: ", err)
		}
		defer pprof.StopCPUProfile()
	}

	powCache = make([]int, 100000)

	s := scanString()
	answer := countDigit(s)
	fmt.Println(answer)

	if *memprofile != "" {
		f, err := os.Create(*memprofile)
		if err != nil {
			log.Fatal("could not create memory profile: ", err)
		}
		defer f.Close() // error handling omitted for example
		runtime.GC()    // get up-to-date statistics
		if err := pprof.WriteHeapProfile(f); err != nil {
			log.Fatal("could not write memory profile: ", err)
		}
	}
}

func debug(a ...interface{}) {
	// fmt.Println(a...)
}

func countDigit(s string) int {
	// 1, 2, 3, 4, 5, 6
	// 10 20 30 40 50 60
	cache := make([][]int, len(s)) // [pos][remainder]
	for i := 0; i < len(s); i++ {
		cache[i] = make([]int, 13)
	}
	for i := len(s) - 1; i >= 0; i-- {
		v := s[i]
		digit := len(s) - 1 - i
		if v == '?' {
			for j := 0; j < 10; j++ {
				currentRemainder := j * pow10(digit) % 13
				// 123:
				//   3 % 13 =  3
				//  23 % 13 = 10
				//  20 % 13 =  7
				// 123 % 13 =  6
				// 100 % 13 =  9
				if digit > 0 {
					for k := 0; k < 13; k++ {
						tmp := (currentRemainder + k) % 13
						debug("i", i, "j", j, "k", k, "current", tmp, currentRemainder, k)
						cache[i][tmp] += cache[i+1][k]
						cache[i][tmp] %= 1000000007
						debug("i", i, "j", j, "k", k, "current", tmp, cache[i][tmp])
					}
				} else {
					debug("i", i, "current", currentRemainder, cache[i][currentRemainder])
					cache[i][currentRemainder]++
					cache[i][currentRemainder] %= 1000000007
					debug("i", i, "current", currentRemainder, cache[i][currentRemainder])
				}
			}
		} else {
			integer, _ := strconv.Atoi(string(v))
			currentRemainder := integer * pow10(digit) % 13

			if digit > 0 {
				for k := 0; k < 13; k++ {
					tmp := (currentRemainder + k) % 13
					debug("i", i, "digit", digit, "k", k, "current", tmp, cache[i][tmp])
					cache[i][tmp] += cache[i+1][k]
					cache[i][tmp] %= 1000000007
					debug("i", i, "digit", digit, "k", k, "current", tmp, cache[i][tmp])
				}
			} else {
				debug("i", i, "current", currentRemainder, cache[i][currentRemainder])
				cache[i][currentRemainder]++
				cache[i][currentRemainder] %= 1000000007
				debug("i", i, "current", currentRemainder, cache[i][currentRemainder])
			}
			debug(cache[i])
		}
	}
	debug(cache)
	return cache[0][5] % 1000000007
}

var powCache []int

func pow10(ex int) int {
	x := 10
	pow := 1
	if powCache[ex] != 0 {
		return powCache[ex]
	}
	for i := 0; i < ex; i++ {
		pow *= x
		pow %= 13
	}
	powCache[ex] = pow
	return pow
}