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

#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int DIV = 1000000007;

int64_t gcdCalc(int64_t a, int64_t b) {
  if (a == 0 && b == 0)
    return 1;
  while (b != 0) {
    int64_t r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int64_t twoPow(int p) {
  if (p == 0)
    return 1;
  int64_t a = twoPow(p / 2);
  int64_t b = (p & 1) ? (a * 2) : a;
  return (a * b) % DIV;
}

int main(int argc, char *argv[]) {
#ifdef VLAD_LOCAL
  ifstream f("in.txt");
  auto start = std::chrono::steady_clock::now();
#else
  istream &f = cin;
#endif
  int n;
  f >> n;
  vector<int64_t> a(n), b(n);
  vector<pair<int64_t, int64_t>> ab(n);
  map<pair<int64_t, int64_t>, int> was;
  for (int i = 0; i < n; i++) {
    f >> a[i] >> b[i];
    int64_t gcd = gcdCalc(abs(a[i]), abs(b[i]));
    ab[i].first = a[i] / gcd;
    ab[i].second = b[i] / gcd;
    pair<int64_t, int64_t> tmp(-ab[i].first, -ab[i].second);
    if (tmp < ab[i])
      ab[i] = tmp;
    auto it = was.find(ab[i]);
    if (it == was.end()) {
      was[ab[i]] = 1;
    } else {
      it->second++;
    }
  }
  int64_t count = 1;
  int add = 0;
  for (auto it = was.begin(); it != was.end(); it++) {
    auto p = it->first;
    int n = it->second;
    if (p.first == 0 && p.second == 0) {
      add = n;
      continue;
    }
    pair<int64_t, int64_t> opp(-p.second, p.first);
    auto tmp = make_pair(-opp.first, -opp.second);
    if (tmp < opp)
      opp = tmp;
    auto itOpp = was.find(opp);
    int64_t cur = n + 1;
    if (itOpp == was.end()) {
      cur = twoPow(n);
    } else {
      if (p < opp) {
        int m = itOpp->second;
        cur = twoPow(n) + twoPow(m) - 1;
        if (cur < 0)
          cur += DIV;
      } else if (opp < p) {
        cur = 1;
      }
    }
    count = (count * cur) % DIV;
  }
  count--;
  if (count < 0)
    count += DIV;
  count = (count + add) % DIV;
  printf("%lld\\n", count);

#ifdef VLAD_LOCAL
  auto end = std::chrono::steady_clock::now();
  double seconds =
      (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count() /
      1000;
  printf("%.3lf\\n", seconds);
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int DIV = 1000000007;

int64_t gcdCalc(int64_t a, int64_t b) {
  while (b != 0) {
    int64_t r = a % b;
    a = b;
    b = r;
  }
  return a;
}

int64_t twoPow(int p) {
  if (p == 0)
    return 1;
  int64_t a = twoPow(p / 2);
  int64_t b = (p & 1) ? (a * 2) : a;
  return (a * b) % DIV;
}

int main(int argc, char *argv[]) {
#ifdef VLAD_LOCAL
  ifstream f("in.txt");
  auto start = std::chrono::steady_clock::now();
#else
  istream &f = cin;
#endif
  int n;
  f >> n;
  vector<int64_t> a(n), b(n);
  vector<pair<int64_t, int64_t>> ab(n);
  map<pair<int64_t, int64_t>, int> was;
  for (int i = 0; i < n; i++) {
    f >> a[i] >> b[i];
    int64_t gcd = gcdCalc(abs(a[i]), abs(b[i]));
    ab[i].first = a[i] / gcd;
    ab[i].second = b[i] / gcd;
    pair<int64_t, int64_t> tmp(-ab[i].first, -ab[i].second);
    if (tmp < ab[i])
      ab[i] = tmp;
    auto it = was.find(ab[i]);
    if (it == was.end()) {
      was[ab[i]] = 1;
    } else {
      it->second++;
    }
  }
  int64_t count = 1;
  for (auto it = was.begin(); it != was.end(); it++) {
    auto p = it->first;
    int n = it->second;
    pair<int64_t, int64_t> opp(-p.second, p.first);
    auto tmp = make_pair(-opp.first, -opp.second);
    if (tmp < opp)
      opp = tmp;
    auto itOpp = was.find(opp);
    int64_t cur = n + 1;
    if (itOpp == was.end()) {
      cur = twoPow(n);
    } else {
      if (p < opp) {
        int m = itOpp->second;
        cur = twoPow(n) + twoPow(m) - 1;
        if (cur < 0)
          cur += DIV;
      } else {
        continue;
      }
    }
    count = (count * cur) % DIV;
  }
  count--;
  if (count < 0)
    count += DIV;
  printf("%lld\\n", count);

#ifdef VLAD_LOCAL
  auto end = std::chrono::steady_clock::now();
  double seconds =
      (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
          .count() /
      1000;
  printf("%.3lf\\n", seconds);
#endif
  return 0;
}