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

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a > b) {
    swap(a, b);
  }

  if (a == 0) {
    return b;
  }

  return gcd(b % a, a);
}

ll pow(int k) {
  ll p = 2;
  ll a = 1;

  for (int i = 0; i < 32; i++) {
    if (((k >> i) & 1) == 1) {
      a = (a * p) % MOD;
    }
    p = (p * p) % MOD;
  }
  return a;
}

int main() {
  int n;
  cin >> n;

  map<pll, int> s;
  ll ans = 0;
  int num = n;

  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {

      num--;
      ans++;
      continue;
    }

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

    if (a == 0 || b == 0) {
      a = abs(a);
      b = abs(b);
    }

    s[make_pair(a, b)]++;
  }

  queue<ll> q;

  for (auto it = s.begin(); it != s.end(); it++) {
    ll a = it->first.first;
    ll b = it->first.second;
    int n0 = it->second;
    // cout << a << " " << b << " " << n0 << endl;

    auto it1 = s.find(make_pair(-b, a));
    if (a == 0) {
      continue;
    }

    if (it1 == s.end()) {
      continue;
    }
    int n1 = it1->second;
    // cout << n1 << endl;

    q.push((pow(n0) + pow(n1) - 1) % MOD);
    num -= (n1 + n0);
  }
  ll a = 1;
  while (!q.empty()) {
    a = (a * q.front()) % MOD;
    q.pop();
  }
  a = (a * pow(num)) % MOD;
  a = (a - 1 + MOD) % MOD;
  ans = (ans + a) % MOD;

  cout << ans << endl;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a > b) {
    swap(a, b);
  }

  if (a == 0) {
    return b;
  }

  return gcd(b % a, a);
}

ll pow(int k) {
  ll p = 2;
  ll a = 1;

  for (int i = 0; i < 32; i++) {
    if (((k >> i) & 1) == 1) {
      a = (a * p) % MOD;
    }
    p = (p * p) % MOD;
  }
  return a;
}

int main() {
  int n;
  cin >> n;

  map<pll, int> s;

  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    ll g = gcd(abs(a), abs(b));
    a /= g;
    b /= g;
    if (a < 0) {
      a *= -1;
      b *= -1;
    }

    s[make_pair(a, b)]++;
  }

  ll ans = (pow(n) - 1 + MOD) % MOD;
  int num = n;
  ll d = 0;
  ll d0 = 0;
  ll d1 = 1;
  for (auto it = s.begin(); it != s.end(); it++) {
    ll a = it->first.first;
    ll b = it->first.second;
    int n0 = it->second;
    // cout << a << " " << b << " " << n0 << endl;

    auto it1 = s.find(make_pair(-b, a));

    if (it1 == s.end()) {
      continue;
    }
    int n1 = it1->second;
    // cout << n1 << endl;
    d0 = (((pow(n0) - 1 + MOD) % MOD * (pow(n1) - 1 + MOD) % MOD) % MOD);
    num -= n0 + n1;
    d = (d + (d1 * d0) % MOD * pow(num)) % MOD;
    d1 = (d1 * (pow(n0 + n1) - d0 + MOD) % MOD) % MOD;
  }
  ans = (ans - d + MOD) % MOD;

  cout << ans << endl;
}