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

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

long long int gdc(long long int a, long long int b) {
  if (b == 0 && a == 0)
    return 1;
  if (b == 0)
    return a;
  if (a == 0)
    return b;
  if (a == b)
    return a;
  if (a > b)
    return gdc(a % b, b);
  return gdc(b % a, a);
}

#define BIGM 1000000007

int modadd(int a, int b) {
  long long int al = (long long int)a;
  long long int bl = (long long int)b;
  if (al + bl >= BIGM)
    return (int)(al + bl - BIGM);
  return (int)(al + bl);
}

int modmul(int a, int b) {
  long long int al = (long long int)a;
  long long int bl = (long long int)b;
  return (int)((al * bl) % BIGM);
}

long long int *_ga;
long long int *_gb;

bool comp(int i1, int i2) {
  if (_ga[i1] < _ga[i2])
    return true;
  if (_ga[i1] > _ga[i2])
    return false;
  return (_gb[i1] < _gb[i2]);
}

bool isEqual(int i1, int i2) {
  return (_ga[i1] == _ga[i2] && _gb[i1] == _gb[i2]);
}

int main(void) {
  int n;
  cin >> n;
  long long int *a = (long long int *)malloc(sizeof(long long int) * n);
  long long int *b = (long long int *)malloc(sizeof(long long int) * n);
  long long int zero = 0;
  _ga = a;
  _gb = b;

  vector<int> l;
  vector<int> r;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];

    if (a[i] == 0 && b[i] == 0) {
      zero++;
      continue;
    } else if (a[i] == 0) {
      b[i] = 1;
      a[i] = 0;
      l.push_back(i);
    } else if (b[i] == 0) {
      b[i] = 1;
      a[i] = 0;
      r.push_back(i);
    } else {
      if (b[i] < 0) {
        b[i] = -b[i];
        a[i] = -a[i];
      }

      if (a[i] < 0) {
        a[i] = -a[i];
        l.push_back(i);
      } else {
        long long int tmp = a[i];
        a[i] = b[i];
        b[i] = tmp;
        r.push_back(i);
      }

      long long int g = gdc(a[i], b[i]);
      a[i] /= g;
      b[i] /= g;
    }
  }

  sort(l.begin(), l.end(), comp);
  sort(r.begin(), r.end(), comp);

  auto litr = l.begin();
  auto ritr = r.begin();
  int ans = 1;
  while (litr != l.end() || ritr != r.end()) {
    if (litr == l.end()) {
      while (ritr != r.end()) {
        ans = modmul(ans, 2);
        *ritr++;
      }
    }
    if (ritr == r.end()) {
      while (litr != l.end()) {
        ans = modmul(ans, 2);
        *litr++;
      }
    } else {

      if (isEqual(*litr, *ritr)) {
        int lcount = 1;
        int lindex = *litr;
        while (litr != l.end() && isEqual(*litr, lindex)) {
          lcount = modmul(lcount, 2);
          litr++;
        }

        int rcount = 1;
        int rindex = *ritr;
        while (ritr != r.end() && isEqual(*ritr, rindex)) {
          rcount = modmul(rcount, 2);
          ritr++;
        }
        int tmpf = modadd(modadd(rcount, lcount), BIGM - 1);
        ans = modmul(ans, tmpf);
      } else if (comp(*litr, *ritr)) {
        ans = modmul(ans, 2);
        litr++;
      } else {
        ans = modmul(ans, 2);
        ritr++;
      }
    }
  }
  ans = modadd(ans, zero);
  cout << modadd(ans, BIGM - 1);
}


Error: WA

Fixed submission:

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

long long int gdc(long long int a, long long int b) {
  if (a == 0)
    return b;
  if (a == b)
    return a;
  if (a > b)
    return gdc(a % b, b);
  return gdc(b % a, a);
}

#define BIGM 1000000007

int modadd(int a, int b) {
  long long int al = (long long int)a;
  long long int bl = (long long int)b;
  if (al + bl >= BIGM)
    return (int)(al + bl - BIGM);
  return (int)(al + bl);
}

int modmul(int a, int b) {
  long long int al = (long long int)a;
  long long int bl = (long long int)b;
  return (int)((al * bl) % BIGM);
}

long long int *_ga;
long long int *_gb;

bool comp(int i1, int i2) {
  if (_ga[i1] < _ga[i2])
    return true;
  if (_ga[i1] > _ga[i2])
    return false;
  return (_gb[i1] < _gb[i2]);
}

bool isEqual(int i1, int i2) {
  return (_ga[i1] == _ga[i2] && _gb[i1] == _gb[i2]);
}

int main(void) {
  int n;
  cin >> n;
  long long int *a = (long long int *)malloc(sizeof(long long int) * n);
  long long int *b = (long long int *)malloc(sizeof(long long int) * n);
  _ga = a;
  _gb = b;

  vector<int> l;
  vector<int> r;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];

    if (b[i] < 0) {
      b[i] = -b[i];
      a[i] = -a[i];
    }

    if (a[i] < 0) {
      a[i] = -a[i];
      l.push_back(i);
    } else {
      long long int tmp = a[i];
      a[i] = b[i];
      b[i] = tmp;
      r.push_back(i);
    }

    long long int g = gdc(a[i], b[i]);
    a[i] /= g;
    b[i] /= g;
  }

  sort(l.begin(), l.end(), comp);
  sort(r.begin(), r.end(), comp);

  auto litr = l.begin();
  auto ritr = r.begin();
  int ans = 1;
  while (litr != l.end() || ritr != r.end()) {
    if (litr == l.end()) {
      while (ritr != r.end()) {
        ans = modmul(ans, 2);
        *ritr++;
      }
    }
    if (ritr == r.end()) {
      while (litr != l.end()) {
        ans = modmul(ans, 2);
        *litr++;
      }
    } else {

      if (isEqual(*litr, *ritr)) {
        int lcount = 1;
        int lindex = *litr;
        while (litr != l.end() && isEqual(*litr, lindex)) {
          lcount = modmul(lcount, 2);
          litr++;
        }

        int rcount = 1;
        int rindex = *ritr;
        while (ritr != r.end() && isEqual(*ritr, rindex)) {
          rcount = modmul(rcount, 2);
          ritr++;
        }
        int tmpf = modadd(modadd(rcount, lcount), BIGM - 1);
        ans = modmul(ans, tmpf);
      } else if (comp(*litr, *ritr)) {
        ans = modmul(ans, 2);
        litr++;
      } else {
        ans = modmul(ans, 2);
        ritr++;
      }
    }
  }

  cout << modadd(ans, BIGM - 1);
}