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

#include <bits/stdc++.h>
using namespace std;

char S[100005][4];

int memo[100005][8][8][8];
char next1[100005][8][8][8];

int N, A, B, C;

int dp(int i, int a, int b, int c) {
  if (a > 7 || b > 7 || c > 7) {
    return 0;
  } else if (i == N) {
    return 1;
  } else if (memo[i][a][b][c] != -1) {
    return memo[i][a][b][c];
  }

  if (S[i][0] == 'A' && S[i][1] == 'C') {
    int ans = 0;
    if (a > 0) {
      if (dp(i + 1, a - 1, b, c + 1)) {
        ans = 1;
        next1[i][a][b][c] = 'C';
      }
    }
    if (c > 0) {
      if (dp(i + 1, a + 1, b, c - 1)) {
        ans = 1;
        next1[i][a][b][c] = 'A';
      }
    }
    return memo[i][a][b][c] = ans;
  }
  if (S[i][0] == 'A' && S[i][1] == 'B') {
    int ans = 0;
    if (a > 0) {
      if (dp(i + 1, a - 1, b + 1, c)) {
        ans = 1;
        next1[i][a][b][c] = 'B';
      }
    }
    if (b > 0) {
      if (dp(i + 1, a + 1, b - 1, c)) {
        ans = 1;
        next1[i][a][b][c] = 'A';
      }
    }
    return memo[i][a][b][c] = ans;
  }
  if (S[i][0] == 'B' && S[i][1] == 'C') {
    int ans = 0;
    if (b > 0) {
      if (dp(i + 1, a, b - 1, c + 1)) {
        ans = 1;
        next1[i][a][b][c] = 'C';
      }
    }
    if (c > 0) {
      if (dp(i + 1, a, b + 1, c - 1)) {
        ans = 1;
        next1[i][a][b][c] = 'B';
      }
    }
    return memo[i][a][b][c] = ans;
  }
}

int main() {
  scanf("%d%d%d%d", &N, &A, &B, &C);

  for (int i = 0; i < N; i++) {
    scanf(" %s", S[i]);
  }

  memset(memo, -1, sizeof(memo));
  int a = min(A, 2);
  int b = min(B, 2);
  int c = min(C, 2);
  int ans = dp(0, a, b, c);

  if (ans == 0) {
    printf("No");
    return 0;
  } else {
    printf("Yes\\n");
    for (int i = 0; i < N; i++) {
      char temp = next1[i][a][b][c];
      printf("%c\\n", temp);
      if (S[i][0] == 'A' && S[i][1] == 'C') {
        if (temp == 'A') {
          a++;
          c--;
        }
        if (temp == 'C') {
          a--;
          c++;
        }
      }
      if (S[i][0] == 'A' && S[i][1] == 'B') {
        if (temp == 'A') {
          a++;
          b--;
        }
        if (temp == 'B') {
          a--;
          b++;
        }
      }
      if (S[i][0] == 'B' && S[i][1] == 'C') {
        if (temp == 'B') {
          b++;
          c--;
        }
        if (temp == 'C') {
          b--;
          c++;
        }
      }
    }
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<long long, long long> pll;

long long A[200005];
long long B[200005];
long long C[200005];
long long D[200005];
long long pow2[200005];

const long long MOD = 1e9 + 7;

map<pll, long long> map1;

void insert1(long long a, long long b, int i) {
  if (b == 0 && a == 0) {
    a = b = 0;
  } else if (b == 0) {
    b = 0;
    a = 1;
  } else if (a == 0) {
    a = 0;
    b = 1;
  } else {
    long long temp = __gcd(abs(a), abs(b));
    a /= temp;
    b /= temp;
  }

  C[i] = a;
  D[i] = b;

  pll indx = pll(a, b);
  // printf("%lld %lld inserted into map\\n", a, b);
  if (map1.find(indx) == map1.end()) {
    map1[indx] = 1;
  } else {
    map1[indx]++;
  }
}

int main() {
  int N;
  scanf("%d", &N);

  pow2[0] = 1;
  for (int i = 1; i <= N; i++) {
    scanf("%lld%lld", &A[i], &B[i]);

    pow2[i] = 2 * pow2[i - 1] % MOD;
  }

  map1[pll(0, 0)] = 0;
  for (int i = 1; i <= N; i++) {
    insert1(A[i], B[i], i);
  }

  long long ans = 1;
  for (int i = 1; i <= N; i++) {
    // printf("C[%d]=%lld D[%d]=%lld\\n", i, C[i], i, D[i]);
    if (C[i] == 0 && D[i] == 0) {
      continue;
    }

    // printf("%lld\\n", map1[pll(C[i], D[i])]);

    long long ways = pow2[map1[pll(C[i], D[i])]];

    map1[pll(C[i], D[i])] = 0;

    pll inv;
    if (C[i] == 0 || D[i] == 0) {
      // ways = pow2[map1[pll(C[i], D[i])]];
      inv = pll(D[i], C[i]);
      if (map1.find(inv) != map1.end()) {
        ways += pow2[map1[inv]];
        map1[inv] = 0;
      } else {
        ways++;
      }
      ways = (ways + MOD - 1) % MOD;
      throw;
    } else {
      inv = pll(-D[i], C[i]);
      if (map1.find(inv) != map1.end()) {
        ways += pow2[map1[inv]];
        map1[inv] = 0;
      } else {
        ways++;
      }

      inv = pll(D[i], -C[i]);
      if (map1.find(inv) != map1.end()) {
        ways += pow2[map1[inv]];
        map1[inv] = 0;
      } else {
        ways++;
      }

      ways = (ways + MOD - 2) % MOD;
    }
    // printf("ways=%lld\\n", ways);
    ans = ans * ways % MOD;
  }
  ans += map1[pll(0, 0)];
  ans = (ans + MOD - 1) % MOD;

  printf("%lld", ans);
  return 0;
}