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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

long long GCD(long long a, long long b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
const int MOD = 1000000007;
using pll = pair<long long, long long>;

int N;
vector<long long> A, B;
long long solve() {
  vector<long long> two(N + 1, 1);
  for (int i = 0; i < N; ++i)
    two[i + 1] = two[i] * 2 % MOD;
  map<pll, long long> neg, pos;
  long long zerozero = 0, azero = 0, bzero = 0;
  for (int i = 0; i < N; ++i) {
    if (A[i] == 0 && B[i] == 0)
      ++zerozero;
    else if (A[i] == 0)
      ++azero;
    else if (B[i] == 0)
      ++bzero;
    else {
      long long g = GCD(A[i], B[i]);
      A[i] /= g, B[i] /= g;
      if (A[i] < 0)
        A[i] = -A[i], B[i] = -B[i];
      if (B[i] > 0)
        pos[pll(A[i], B[i])]++;
      else
        neg[pll(-B[i], A[i])]++;
    }
  }
  vector<pll> alt;
  for (auto it : neg)
    alt.push_back(it.first);
  for (auto it : pos)
    alt.push_back(it.first);
  sort(alt.begin(), alt.end());
  alt.erase(unique(alt.begin(), alt.end()), alt.end());
  long long res = (two[azero] + two[bzero] - 1 + MOD) % MOD;
  for (auto p : alt) {
    long long tmp = (two[neg[p]] + two[pos[p]] - 1 + MOD) % MOD;
    res = (res * tmp) % MOD;
  }
  res = (res + zerozero - 1 + MOD) % MOD;

  // COUT(zero); COUT(alt); COUT(neg); COUT(pos);

  return res;
}

int main() {
  while (cin >> N) {
    A.resize(N);
    B.resize(N);
    for (int i = 0; i < N; ++i)
      cin >> A[i] >> B[i];
    cout << solve() << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

long long GCD(long long a, long long b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (b == 0)
    return a;
  else
    return GCD(b, a % b);
}
const int MOD = 1000000007;
using pll = pair<long long, long long>;

int N;
vector<long long> A, B;
long long solve() {
  vector<long long> two(N + 1, 1);
  for (int i = 0; i < N; ++i)
    two[i + 1] = two[i] * 2 % MOD;
  map<pll, long long> neg, pos;
  long long zero = 0;
  for (int i = 0; i < N; ++i) {
    long long g = GCD(A[i], B[i]);
    A[i] /= g, B[i] /= g;
    if (A[i] < 0)
      A[i] = -A[i], B[i] = -B[i];

    if (A[i] == 0 || B[i] == 0)
      ++zero;
    else if (B[i] > 0)
      pos[pll(A[i], B[i])]++;
    else
      neg[pll(-B[i], A[i])]++;
  }
  vector<pll> alt;
  for (auto it : neg)
    alt.push_back(it.first);
  for (auto it : pos)
    alt.push_back(it.first);
  sort(alt.begin(), alt.end());
  alt.erase(unique(alt.begin(), alt.end()), alt.end());
  long long res = zero + 1;
  for (auto p : alt) {
    long long tmp = (two[neg[p]] + two[pos[p]] - 1) % MOD;
    res = (res * tmp) % MOD;
  }

  /*
      COUT(zero);
      COUT(alt);
      COUT(neg);
      COUT(pos);
  */
  return (res + MOD - 1) % MOD;
}

int main() {
  while (cin >> N) {
    A.resize(N);
    B.resize(N);
    for (int i = 0; i < N; ++i)
      cin >> A[i] >> B[i];
    cout << solve() << endl;
  }
}