Problem p01889 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

const int MOD = 1E9 + 7;

const int NMAT[7][7] = {{1, 0, 0, 0, 0, 0, 0},  {0, 1, 0, 0, 0, 0, 0},
                        {0, 0, 1, -1, 0, 0, 0}, {0, 0, 0, 10, 0, 0, 0},
                        {0, 0, 0, 0, 1, -1, 0}, {0, 0, 0, 0, 0, 10, 0},
                        {0, 0, 0, 0, 0, 0, 1}};

const int MMAT[7][7] = {
    {1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0},
    {0, 0, 0, 0, 0, 0, 1},
};

const int AMAT[7][7] = {{1, 0, 0, 0, -1, 0, 0}, {0, 0, 0, 0, 0, 0, 1},
                        {0, 0, 0, 0, 0, 0, 0},  {0, 0, 0, 0, 0, 0, 1},
                        {0, 0, 0, 0, 0, 0, 0},  {0, 0, 0, 0, 0, 0, 1},
                        {0, 0, 0, 0, 0, 0, 1}};

struct mat {
  int d[7][7];
  mat() {
    for (int i = 0; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        d[i][j] = 0;
  }
  int *operator[](int x) { return d[x]; }
  int const *operator[](int x) const { return d[x]; }
};

mat operator*(const mat &a, const mat &b) {
  mat res;
  for (int i = 0; i < 7; ++i)
    for (int j = 0; j < 7; ++j)
      for (int k = 0; k < 7; ++k)
        res[i][j] = (res[i][j] + 1ll * a[i][k] * b[k][j]) % MOD;
  return res;
}

mat id() {
  mat res;
  for (int i = 0; i < 7; ++i)
    res[i][i] = 1;
  return res;
}

mat nmat[10], mmat, amat, smat;

void init() {
  for (int digit = 0; digit < 10; ++digit)
    for (int i = 0; i < 7; ++i)
      for (int j = 0; j < 7; ++j)
        nmat[digit][i][j] = (NMAT[i][j] < 0) ? digit : NMAT[i][j];
  for (int i = 0; i < 7; ++i)
    for (int j = 0; j < 7; ++j)
      mmat[i][j] = MMAT[i][j];
  for (int i = 0; i < 7; ++i)
    for (int j = 0; j < 7; ++j)
      amat[i][j] = (AMAT[i][j] < 0) ? 1 : AMAT[i][j];
  for (int i = 0; i < 7; ++i)
    for (int j = 0; j < 7; ++j)
      smat[i][j] = (AMAT[i][j] < 0) ? MOD - 1 : AMAT[i][j];
}

int N;
int R[11000];
char S[11000][20];

int ans[7] = {0, 1, 0, 1, 0, 1, 1}, tmp[7];

int main() {
  init();
  scanf("%d", &N);
  for (int i = 0; i < N; ++i)
    scanf("%d %s", &R[i], S[i]);
  for (int i = N - 1; i >= 0; --i) {
    mat mul = id(), ter = id();
    for (int j = strlen(S[i]) - 1; j >= 0; --j) {
      if (S[i][j] >= '0' && S[i][j] <= '9')
        mul = nmat[S[i][j] - '0'] * mul;
      else if (S[i][j] == '*')
        mul = mmat * mul;
      else if (S[i][j] == '+')
        mul = amat * mul;
      else
        mul = smat * mul;
    }
    while (R[i]) {
      if (R[i] & 1)
        ter = mul * ter;
      mul = mul * mul;
      R[i] >>= 1;
    }
    for (int i = 0; i < 7; ++i) {
      tmp[i] = 0;
      for (int j = 0; j < 7; ++j)
        tmp[i] = (tmp[i] + 1ll * ans[j] * ter[i][j]) % MOD;
    }
    for (int i = 0; i < 7; ++i)
      ans[i] = tmp[i];
  }
  for (int i = 0; i < 7; ++i) {
    tmp[i] = 0;
    for (int j = 0; j < 7; ++j)
      tmp[i] = (tmp[i] + 1ll * ans[j] * amat[i][j]) % MOD;
  }
  for (int i = 0; i < 7; ++i)
    ans[i] = tmp[i];
  printf("%d\\n", ans[0]);
}

Error: 0

Fixed submission:

/**************************************************************************************************************
 *  Md. Abdulla Al Mamun (Nayon)
 *  ID: 1306001
 *  Session: 2013-2014
 *  Department of Computer Science and Engineering
 *  Begum Rokeya University, Rangpur (BRUR)
 ***************************************************************************************************************/
#include <bits/stdc++.h>
using namespace std;

#define PI acos(-1.0)
#define EPS 1e-9
#define INF 1 << 28
#define sq(a) ((a) * (a))
#define toRad(a) ((a) * (PI) / 180)
#define toDeg(a) ((a)*180 / (PI))
#define all(x) (x).begin(), (x).end()
#define pb(x) push_back(x)
#define mp(a, b) make_pair((a), (b))
#define endl '\\n'
#define MAX 100000
#define MOD 1000000007
#define what_is(x) cerr << #x << " is " << x << endl;
#define mset(array, value) memset(array, value, sizeof(array))

inline bool isEq(double a, double b) { return (abs(a - b) < EPS); }

typedef long long ll;
typedef pair<int, int> pii;
inline ll negModHandler(ll a, ll d) {
  return ((a % d) < 0) ? d + (a % d) : (a % d);
}

// #define isValid(a, b) ((a >= 0 && a < b) ? 1 : 0)
// int dr[]  =  {0, -1, -1, -1,  0,  1, 1, 1};
// int dc[]  =  {1,  1,  0, -1, -1, -1, 0, 1};

string strMod(string s) {
  ll n = 0, len = s.length();
  for (int i = 0; i < len; i++) {
    n = (n * 10 + s[i] - '0') % MOD;
  }
  stringstream ss;
  ss << n;
  ss >> s;
  return s;
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, r, i;
  string str, all;
  stringstream ss, ss2;
  cin >> n;
  all = "";
  while (n--) {
    cin >> r >> str;
    for (i = 0; i < r; i++) {
      all += str;
    }
  }
  // cout << "# " << all << endl;
  int ln = all.length();
  string all2 = "";
  str = "";
  for (i = 0; i < ln; i++) {
    while (i < ln && isdigit(all[i])) {
      str += all[i];
      i++;
    }
    while (i < ln && all[i] == '*') {
      string str2 = "";
      i++;
      while (isdigit(all[i])) {
        str2 += all[i];
        i++;
      }
      ss.clear();
      ss2.clear();
      ss << strMod(str);
      ss2 << strMod(str2);
      ll n1, n2;
      ss >> n1;
      ss2 >> n2;
      n1 = (n1 % MOD * n2 % MOD) % MOD;
      ss.clear();
      ss << n1;
      ss >> str;
    }
    if (all[i] == '-' || all[i] == '+') {
      str += all[i];
    }
    all2 += str;
    str = "";
  }
  // cout << "# " << all2 << endl;
  ln = all2.length();
  ll ans = 0;
  str = "";
  i = 0;
  while (i < ln && isdigit(all2[i])) {
    str += all2[i];
    i++;
  }
  ss.clear();
  ss << strMod(str);
  ss >> ans;
  if (i < ln) {
    char sign = all2[i];
    for (i++; i < ln; i++) {
      str = "";
      while (i < ln && isdigit(all2[i])) {
        str += all2[i];
        i++;
      }
      ll n1;
      ss.clear();
      ss << strMod(str);
      ss >> n1;
      if (sign == '+') {
        ans = negModHandler(ans + n1, MOD);
      } else {
        ans = negModHandler(ans - n1, MOD);
      }
      if (i < ln)
        sign = all2[i];
    }
  }
  cout << ans << endl;
  return 0;
}