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

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;

const int mod = 1e9 + 7;

struct P {
  int A, T, G, C;
  P(int a, int b, int c, int d) {
    A = a;
    T = b;
    G = c;
    C = d;
  }
};

int A, T, G, C, n;
string s[51], t, name;
map<string, string> M;
int dp[55][55][55][55];
int CNT[222][4], cnt;

void END() {
  cout << 0 << endl;
  exit(0);
}

int main() {
  cin >> A >> T >> G >> C >> n;
  getline(cin, s[0]);
  r(i, n) getline(cin, s[n - i - 1]);
  r(i, n) {
    stringstream ss(s[i]);
    ss >> name;
    string arr;
    while (ss >> t) {
      if (t[0] == '[')
        arr += t;
      else
        arr += M[t + ':'];
    }
    M[name] = arr;
    if (arr.size() > 200 * 6)
      END();
  }
  t = M[name];
  set<char> st;
  r(i, t.size()) {
    if (t[i] == ']') {
      if (st.count('A'))
        CNT[cnt][0] = 1;
      if (st.count('T'))
        CNT[cnt][1] = 1;
      if (st.count('G'))
        CNT[cnt][2] = 1;
      if (st.count('C'))
        CNT[cnt][3] = 1;
      st.clear();
      cnt++;
    } else
      st.insert(t[i]);
  }
  n = A + T + C + G;
  if (n != cnt)
    END();
  queue<P> q;
  q.push(P(A, T, G, C));
  dp[A][T][G][C] = 1;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    A = p.A;
    T = p.T;
    G = p.G;
    C = p.C;
    cnt = dp[A][T][G][C];
    int idx = n - (A + T + G + C);
    if (A && CNT[idx][0]) {
      if (!dp[A - 1][T][G][C])
        q.push(P(A - 1, T, G, C));
      dp[A - 1][T][G][C] += cnt;
      dp[A - 1][T][G][C] %= mod;
    }
    if (T && CNT[idx][1]) {
      if (!dp[A][T - 1][G][C])
        q.push(P(A, T - 1, G, C));
      dp[A][T - 1][G][C] += cnt;
      dp[A][T - 1][G][C] %= mod;
    }
    if (G && CNT[idx][2]) {
      if (!dp[A][T][G - 1][C])
        q.push(P(A, T, G - 1, C));
      dp[A][T][G - 1][C] += cnt;
      dp[A][T][G - 1][C] %= mod;
    }
    if (C && CNT[idx][3]) {
      if (!dp[A][T][G][C - 1])
        q.push(P(A, T, G, C - 1));
      dp[A][T][G][C - 1] += cnt;
      dp[A][T][G][C - 1] %= mod;
    }
  }
  cout << dp[0][0][0][0] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;

struct P {
  int A, T, G, C;
  P(int a, int b, int c, int d) {
    A = a;
    T = b;
    G = c;
    C = d;
  }
};

int A, T, G, C, n;
string s[51], t, name;
map<string, string> M;
int dp[55][55][55][55];
int CNT[222][4], cnt;

int main() {
  cin >> A >> T >> G >> C >> n;
  getline(cin, s[0]);
  r(i, n) getline(cin, s[n - i - 1]);
  r(i, n) {
    stringstream ss(s[i]);
    ss >> name;
    string arr;
    while (ss >> t) {
      if (t[0] == '[')
        arr += t;
      else
        arr += M[t + ':'];
    }
    M[name] = arr;
  }
  t = M[name];
  set<char> st;
  r(i, t.size()) {
    if (t[i] == ']') {
      if (st.count('A'))
        CNT[cnt][0] = 1;
      if (st.count('T'))
        CNT[cnt][1] = 1;
      if (st.count('G'))
        CNT[cnt][2] = 1;
      if (st.count('C'))
        CNT[cnt][3] = 1;
      st.clear();
      cnt++;
    } else
      st.insert(t[i]);
  }
  n = A + T + C + G;
  queue<P> q;
  q.push(P(A, T, G, C));
  dp[A][T][G][C] = 1;
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    A = p.A;
    T = p.T;
    G = p.G;
    C = p.C;
    cnt = dp[A][T][G][C];
    int idx = n - (A + T + G + C);
    if (A && CNT[idx][0]) {
      if (!dp[A - 1][T][G][C])
        q.push(P(A - 1, T, G, C));
      dp[A - 1][T][G][C] += cnt;
    }
    if (T && CNT[idx][1]) {
      if (!dp[A][T - 1][G][C])
        q.push(P(A, T - 1, G, C));
      dp[A][T - 1][G][C] += cnt;
    }
    if (G && CNT[idx][2]) {
      if (!dp[A][T][G - 1][C])
        q.push(P(A, T, G - 1, C));
      dp[A][T][G - 1][C] += cnt;
    }
    if (C && CNT[idx][3]) {
      if (!dp[A][T][G][C - 1])
        q.push(P(A, T, G, C - 1));
      dp[A][T][G][C - 1] += cnt;
    }
  }
  cout << dp[0][0][0][0] << endl;
}