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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define MOD 1000000007
#define PI 3.14159265359

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll mypow(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  multiset<pair<ll, ll>> dir1;
  multiset<pair<ll, ll>> dir2;
  vector<int> num;
  int numzero = 0;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      numzero++;
      continue;
    } else if (b == 0) {
      dir1.insert(make_pair(1, 0));
      continue;
    } else if (a == 0) {
      dir2.insert(make_pair(1, 0));
      continue;
    }

    ll g = gcd(abs(a), abs(b));
    a = a / g;
    b = b / g;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }

    if (b >= 0)
      dir1.insert(make_pair(a, b));
    else
      dir2.insert(make_pair(-b, a));
  }
  ll res = 1;

  multiset<pair<ll, ll>>::iterator it1 = dir1.begin();
  multiset<pair<ll, ll>>::iterator it2 = dir2.begin();

  while (it1 != dir1.end() || it2 != dir2.end()) {
    ll count = 0;
    if (it1 == dir1.end()) {
      count = mypow(2, dir2.count(*it2)) % MOD;
      it2 = dir2.upper_bound(*it2);
    } else if (it2 == dir2.end()) {
      count = mypow(2, dir1.count(*it1)) % MOD;
      it1 = dir1.upper_bound(*it1);
    } else if (*it2 < *it1) {
      count = mypow(2, dir2.count(*it2)) % MOD;
      it2 = dir2.upper_bound(*it2);
    } else if (*it1 < *it2) {
      count = mypow(2, dir1.count(*it1)) % MOD;
      it1 = dir1.upper_bound(*it1);
    } else {
      count = (mypow(2, dir2.count(*it2)) - 1) % MOD;
      count = (count + mypow(2, dir1.count(*it1)) - 1) % MOD;
      count = (count + 1) % MOD;
      if (count < 0)
        count + MOD;

      it1 = dir1.upper_bound(*it1);
      it2 = dir2.upper_bound(*it2);
    }
    res = res * count % MOD;
  }

  res = (res - 1 + numzero) % MOD;
  if (res < 0)
    res += MOD;
  cout << res << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

#define MOD 1000000007
#define PI 3.14159265359

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll mypow(ll a, ll b) {
  a %= MOD;
  ll res = 1;
  while (b > 0) {
    if (b % 2 == 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  multiset<pair<ll, ll>> dir1;
  multiset<pair<ll, ll>> dir2;
  vector<int> num;
  int numzero = 0;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      numzero++;
      continue;
    }
    ll g = gcd(a, b);
    a = a / g;
    b = b / g;
    if (a < 0) {
      a *= -1;
      b *= -1;
    } else if (a == 0 && b < 0)
      b *= -1;

    if (b >= 0)
      dir1.insert(make_pair(a, b));
    else
      dir2.insert(make_pair(-b, a));
  }
  int res = 1;

  multiset<pair<ll, ll>>::iterator it1 = dir1.begin();
  multiset<pair<ll, ll>>::iterator it2 = dir2.begin();

  while (it1 != dir1.end() || it2 != dir2.end()) {
    ll count = 0;
    if (it1 == dir1.end()) {
      count = mypow(2, dir2.count(*it2)) % MOD;
      it2 = dir2.upper_bound(*it2);
    } else if (it2 == dir2.end()) {
      count = mypow(2, dir1.count(*it1)) % MOD;
      it1 = dir1.upper_bound(*it1);
    } else if (*it2 < *it1) {
      count = mypow(2, dir2.count(*it2)) % MOD;
      it2 = dir2.upper_bound(*it2);
    } else if (*it1 < *it2) {
      count = mypow(2, dir1.count(*it1)) % MOD;
      it1 = dir1.upper_bound(*it1);
    } else {
      count += mypow(2, dir2.count(*it2)) % MOD;
      count += mypow(2, dir1.count(*it1)) % MOD;
      count -= 1 % MOD;

      it1 = dir1.upper_bound(*it1);
      it2 = dir2.upper_bound(*it2);
    }
    res = res * count % MOD;
  }

  if (numzero == 0)
    cout << res - 1 << endl;
  else {
    ll reszero = numzero;
    res = res - 1 + reszero % MOD;
    cout << res << endl;
  }
}