Problem p01551 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int mod = 1e9 + 7;
void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

int A, B, C, D, N;
map<string, int> mp;
string nam[50];
vector<string> vst[50];
int num[50][16];
int dp[51][52][52][52];

int main() {
  cin >> A >> B >> C >> D >> N;
  {
    string st;
    int I = -1;
    while (cin >> st) {
      if (st.back() == ':') {
        I++;
        nam[I] = st.substr(0, st.size() - 1);
        mp[nam[I]] = I;
      } else {
        vst[I].pb(st);
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    for (string st : vst[i]) {
      if (st[0] == '[') {
        int bit = 0;
        for (char ch : st) {
          if (ch == 'A')
            bit |= 1;
          if (ch == 'T')
            bit |= 2;
          if (ch == 'G')
            bit |= 4;
          if (ch == 'C')
            bit |= 8;
        }
        num[i][bit]++;
      } else {
        int id = mp[st];
        rep(j, 16) num[i][j] += num[id][j];
      }
    }
  }
  int S = 0;
  rep(i, 16) S += num[0][i];
  if (S != A + B + C + D) {
    puts("0");
    return 0;
  }
  vector<int> bs;
  rep(i, 16) rep(j, num[0][i]) bs.pb(i);
  dp[0][0][0][0] = 1;
  rep(i, S) {
    rep(a, A + 1) rep(b, B + 1) rep(c, C + 1) {
      int d = i - a - b - c;
      if (d < 0 || d > D)
        continue;
      int x = bs[i];
      rep(j, 4) {
        if ((x >> j) & 1)
          add(dp[i + 1][a + (j == 0)][b + (j == 1)][c + (j == 2)],
              dp[i][a][b][c]);
      }
    }
  }
  cout << dp[S][A][B][C] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
int mod = 1e9 + 7;
void add(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

int A, B, C, D, N;
map<string, int> mp;
string nam[50];
vector<string> vst[50];
int num[50][16];
int dp[201][52][52][52];

int main() {
  cin >> A >> B >> C >> D >> N;
  {
    string st;
    int I = -1;
    while (cin >> st) {
      if (st.back() == ':') {
        I++;
        nam[I] = st.substr(0, st.size() - 1);
        mp[nam[I]] = I;
      } else {
        vst[I].pb(st);
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    for (string st : vst[i]) {
      if (st[0] == '[') {
        int bit = 0;
        for (char ch : st) {
          if (ch == 'A')
            bit |= 1;
          if (ch == 'T')
            bit |= 2;
          if (ch == 'G')
            bit |= 4;
          if (ch == 'C')
            bit |= 8;
        }
        num[i][bit]++;
      } else {
        int id = mp[st];
        rep(j, 16) num[i][j] += num[id][j];
      }
    }
  }
  int S = 0;
  rep(i, 16) S += num[0][i];
  if (S != A + B + C + D) {
    puts("0");
    return 0;
  }
  vector<int> bs;
  rep(i, 16) rep(j, num[0][i]) bs.pb(i);
  dp[0][0][0][0] = 1;
  rep(i, S) {
    rep(a, A + 1) rep(b, B + 1) rep(c, C + 1) {
      int d = i - a - b - c;
      if (d < 0 || d > D)
        continue;
      int x = bs[i];
      rep(j, 4) {
        if ((x >> j) & 1)
          add(dp[i + 1][a + (j == 0)][b + (j == 1)][c + (j == 2)],
              dp[i][a][b][c]);
      }
    }
  }
  cout << dp[S][A][B][C] << endl;
}