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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

ll mod = 1e9 + 7;
ll dp[210][55][55][55] = {0};
ll add(ll a, ll b) { return (a + b) % mod; }
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int A, T, G, C;
  cin >> A >> T >> G >> C;
  int M;
  cin >> M;
  vector<vector<string>> v(M);
  map<string, int> tbl;
  {
    string str;
    int p = -1;
    while (cin >> str) {
      int n = str.size();
      if (str[n - 1] == ':') {
        tbl[str.substr(0, n - 1)] = ++p;
      } else {
        v[p].pb(str);
      }
    }
  }
  map<char, int> it{{'A', 0}, {'T', 1}, {'G', 2}, {'C', 3}};
  vector<vector<vector<int>>> ig(M);
  int S = A + T + G + C;
  RREP(i, M) {
    EACH(str, v[i]) {
      if (ig[i].size() > S)
        break;
      if (str[0] == '[') {
        vector<int> item(4, 0);
        str = str.substr(1, (int)str.size() - 2);
        REP(j, str.size()) { item[it[str[j]]] = 1; }
        ig[i].pb(item);
      } else {
        EACH(item, ig[tbl[str]]) {
          ig[i].pb(item);
          if (ig[i].size() > S)
            break;
        }
      }
    }
  }
  vector<vector<int>> items = ig[0];
  int N = items.size();
  if (N != S) {
    cout << 0 << endl;
    return 0;
  }
  //	cout << items << endl;
  dp[0][0][0][0] = 1;
  REP(i, N) REP(a, A + 1) REP(t, T + 1) REP(g, G + 1) {
    int c = i - (a + t + g);
    if (c < 0)
      continue;
    if (items[i][0] && a + 1 <= A) {
      dp[i + 1][a + 1][t][g] = add(dp[i + 1][a + 1][t][g], dp[i][a][t][g]);
    }
    if (items[i][1] && t + 1 <= T) {
      dp[i + 1][a][t + 1][g] = add(dp[i + 1][a][t + 1][g], dp[i][a][t][g]);
    }
    if (items[i][2] && g + 1 <= G) {
      dp[i + 1][a][t][g + 1] = add(dp[i + 1][a][t][g + 1], dp[i][a][t][g]);
    }
    if (items[i][3] && c + 1 <= C) {
      dp[i + 1][a][t][g] = add(dp[i + 1][a][t][g], dp[i][a][t][g]);
    }
  }
  cout << dp[N][A][T][G] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

ll mod = 1e9 + 7;
ll dp[210][55][55][55] = {0};
ll add(ll a, ll b) { return (a + b) % mod; }
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int A, T, G, C;
  cin >> A >> T >> G >> C;
  int M;
  cin >> M;
  vector<vector<string>> v(M);
  map<string, int> tbl;
  {
    string str;
    int p = -1;
    while (cin >> str) {
      int n = str.size();
      if (str[n - 1] == ':') {
        tbl[str.substr(0, n - 1)] = ++p;
      } else {
        v[p].pb(str);
      }
    }
  }
  map<char, int> it{{'A', 0}, {'T', 1}, {'G', 2}, {'C', 3}};
  vector<vector<vector<int>>> ig(M);
  RREP(i, M) {
    EACH(str, v[i]) {
      if (str[0] == '[') {
        vector<int> item(4, 0);
        str = str.substr(1, (int)str.size() - 2);
        REP(j, str.size()) { item[it[str[j]]] = 1; }
        ig[i].pb(item);
      } else {
        EACH(item, ig[tbl[str]]) { ig[i].pb(item); }
      }
    }
  }
  int S = A + T + G + C;
  vector<vector<int>> items = ig[0];
  int N = items.size();
  if (N != S) {
    cout << 0 << endl;
    return 0;
  }
  //	cout << items << endl;
  dp[0][0][0][0] = 1;
  REP(i, N) REP(a, A + 1) REP(t, T + 1) REP(g, G + 1) {
    int c = i - (a + t + g);
    if (c < 0)
      continue;
    if (items[i][0] && a + 1 <= A) {
      dp[i + 1][a + 1][t][g] = add(dp[i + 1][a + 1][t][g], dp[i][a][t][g]);
    }
    if (items[i][1] && t + 1 <= T) {
      dp[i + 1][a][t + 1][g] = add(dp[i + 1][a][t + 1][g], dp[i][a][t][g]);
    }
    if (items[i][2] && g + 1 <= G) {
      dp[i + 1][a][t][g + 1] = add(dp[i + 1][a][t][g + 1], dp[i][a][t][g]);
    }
    if (items[i][3] && c + 1 <= C) {
      dp[i + 1][a][t][g] = add(dp[i + 1][a][t][g], dp[i][a][t][g]);
    }
  }
  cout << dp[N][A][T][G] << endl;
}