Problem p02960 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define REPx(x, a) for (auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTG(a) sort(ALL(a), greater<int>())
#define REVERSE(a) reverse(ALL(a))
#define bit_search(bit, n) REP(bit, 1 << n)
#define bit_check(bit, i) (bit >> i) & 1
#define setpre(n) fixed << setprecision(n)
#define ENDL cout << endl;
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VP = vector<PII>;
using VI = vector<int>;
using VVI = vector<VI>;
using Vll = vector<ll>;
using VVll = vector<Vll>;
using VC = vector<char>;
using VS = vector<string>;
using VB = vector<bool>;
const int INF = 1e9;
const ll mod = 1e9 + 7;

int POW(ll a, ll b, ll m) {
  ll res = 1;
  REP(i, (int)b) {
    res *= a;
    res %= m;
  }
  return (int)res;
}

int main() {
  string S;
  cin >> S;
  ll len = S.size();
  VVll dp(len, Vll(13, 0));
  REPR(i, len) {
    if (i == len - 1) {
      if (S.at(i) != '?') {
        REP(j, 13) if (S.at(i) - '0' == j) dp.at(i).at(j) = 1;
      } else
        REP(j, 10) dp.at(i).at(j) = 1;
      continue;
    }
    REP(j, 13) {
      if (S.at(i) == '?') {
        REP(s_i, 10) {
          dp.at(i).at(j) += dp.at(i + 1).at(
              (13 + j - ((s_i * POW(10, len - i - 1, 13)) % 13)) % 13);
          dp.at(i).at(j) %= mod;
        }
      } else {
        dp.at(i).at(j) += dp.at(i + 1).at(
            (13 + j - (((S.at(i) - '0') * POW(10, len - i - 1, 13)) % 13)) %
            13);
        dp.at(i).at(j) %= mod;
      }
    }
  }
  cout << dp.at(0).at(5) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define REPx(x, a) for (auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTG(a) sort(ALL(a), greater<int>())
#define REVERSE(a) reverse(ALL(a))
#define bit_search(bit, n) REP(bit, 1 << n)
#define bit_check(bit, i) (bit >> i) & 1
#define setpre(n) fixed << setprecision(n)
#define ENDL cout << endl;
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VP = vector<PII>;
using VI = vector<int>;
using VVI = vector<VI>;
using Vll = vector<ll>;
using VVll = vector<Vll>;
using VC = vector<char>;
using VS = vector<string>;
using VB = vector<bool>;
const int INF = 1e9;
const ll mod = 1e9 + 7;

int POW(ll a, ll b, ll m) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return (int)res;
}

int main() {
  string S;
  cin >> S;
  ll len = S.size();
  VVll dp(len, Vll(13, 0));
  REPR(i, len) {
    if (i == len - 1) {
      if (S.at(i) != '?') {
        REP(j, 13) if (S.at(i) - '0' == j) dp.at(i).at(j) = 1;
      } else
        REP(j, 10) dp.at(i).at(j) = 1;
      continue;
    }
    REP(j, 13) {
      if (S.at(i) == '?') {
        REP(s_i, 10) {
          dp.at(i).at(j) += dp.at(i + 1).at(
              (13 + j - ((s_i * POW(10, len - i - 1, 13)) % 13)) % 13);
          dp.at(i).at(j) %= mod;
        }
      } else {
        dp.at(i).at(j) += dp.at(i + 1).at(
            (13 + j - (((S.at(i) - '0') * POW(10, len - i - 1, 13)) % 13)) %
            13);
        dp.at(i).at(j) %= mod;
      }
    }
  }
  cout << dp.at(0).at(5) << endl;

  return 0;
}