Problem p02960 (C++)

Buggy submission (status: Runtime Error):
//------------------------------------------
// C++ templete
//------------------------------------------
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
using ll = long long;

#define endl "\n"

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// REPEAT
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// container util
//------------------------------------------
#define pb push_back
#define paired make_pair
#define ALL(a) (a).begin(), (a).end()
#define PRINT(V)                                                               \
  for (auto v : (V))                                                           \
  cout << v << " "
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())

// constant
//------------------------------------------
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// math
//------------------------------------------
int QP(int a, int b) {
  int ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % MOD;
    a = 1ll * a * a % MOD;
  } while (b >>= 1);
  return ans;
}
int QP(int a, int b, int MOD) {
  int ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % MOD;
    a = 1ll * a * a % MOD;
  } while (b >>= 1);
  return ans;
}
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }

// debug
//------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// 動的計画法で役に立つ関数
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int calc(int k, int i) {
  // k*10**(i+1)を13でわったあまり
  int n = i % 6;
  VI V{10, 9, 12, 3, 4, 1};
  int ans = V[n];
  ans = ans * k % 13;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  string S;
  cin >> S;
  ll dp[10005][13];

  char c = S.back();
  int N = S.size();
  // initislization
  REP(i, 13) {
    dp[0][i] = 0;
    if (c == '?') {
      if (i <= 9)
        dp[0][i] = 1;
    } else {
      if (i == c - 48) {
        dp[0][i] = 1;
      }
    }
  }
  // update
  REP(i, N - 1) {
    c = S[N - 2 - i];
    REP(j, 13) {
      dp[i + 1][j] = 0;
      if (c == '?') {
        REP(k, 10) {
          int num = j - calc(k, i);
          int index = num >= 0 ? num : 13 + num;
          dp[i + 1][j] += dp[i][index];
        }
      } else {
        int num = j - calc((int)c - 48, i);
        int index = num >= 0 ? num : 13 + num;
        dp[i + 1][j] += dp[i][index];
      }
      dp[i + 1][j] = dp[i + 1][j] % MOD;
    }
  }
  cout << dp[N - 1][5] << endl;
  return 0;
}


Fixed submission:
//------------------------------------------
// C++ templete
//------------------------------------------
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
using ll = long long;

#define endl "\n"

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

// REPEAT
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// container util
//------------------------------------------
#define pb push_back
#define paired make_pair
#define ALL(a) (a).begin(), (a).end()
#define PRINT(V)                                                               \
  for (auto v : (V))                                                           \
  cout << v << " "
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())

// constant
//------------------------------------------
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

// math
//------------------------------------------
int QP(int a, int b) {
  int ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % MOD;
    a = 1ll * a * a % MOD;
  } while (b >>= 1);
  return ans;
}
int QP(int a, int b, int MOD) {
  int ans = 1;
  do {
    if (b & 1)
      ans = 1ll * ans * a % MOD;
    a = 1ll * a * a % MOD;
  } while (b >>= 1);
  return ans;
}
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }

// debug
//------------------------------------------
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

// 動的計画法で役に立つ関数
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int calc(int k, int i) {
  // k*10**(i+1)を13でわったあまり
  int n = i % 6;
  VI V{10, 9, 12, 3, 4, 1};
  int ans = V[n];
  ans = ans * k % 13;
  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  string S;
  cin >> S;
  unsigned long long dp[100050][13];

  char c = S.back();
  int N = S.size();
  // initislization
  REP(i, 13) {
    dp[0][i] = 0;
    if (c == '?') {
      if (i <= 9)
        dp[0][i] = 1;
    } else {
      if (i == c - 48) {
        dp[0][i] = 1;
      }
    }
  }
  // update
  REP(i, N - 1) {
    c = S[N - 2 - i];
    REP(j, 13) {
      dp[i + 1][j] = 0;
      if (c == '?') {
        REP(k, 10) {
          int num = j - calc(k, i);
          int index = num >= 0 ? num : 13 + num;
          dp[i + 1][j] += dp[i][index];
        }
      } else {
        int num = j - calc((int)c - 48, i);
        int index = num >= 0 ? num : 13 + num;
        dp[i + 1][j] += dp[i][index];
      }
      dp[i + 1][j] = dp[i + 1][j] % MOD;
    }
  }
  cout << dp[N - 1][5] << endl;
  return 0;
}