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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (ll i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<ll>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

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

ll mod = 1000000007;

ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = modpow(a, b / 2);
    return d * d % mod;
  }
  return a * modpow(a, b - 1) % mod;
}

ll divide(ll a, ll b) { return a * modpow(b, mod - 2) % mod; }

int main() {
  int n = in();
  vector<pll> a(n);
  rep(i, n) { a[i] = mp(lin(), lin()); }
  int cnt0 = 0;
  map<pll, int> m;
  rep(i, n) {
    ll x = a[i].first;
    ll y = a[i].second;
    ll g = gcd(abs(x), abs(y));
    if (g > 0) {
      x /= g;
      y /= g;
    } else {
      cnt0++;
    }
    if (x < 0) {
      x = -x;
      y = -y;
    }
    m[mp(x, y)]++;
  }
  ll ans = 1;
  auto itr = m.begin();
  while (itr != m.end()) {
    ll x = itr->first.first;
    ll y = itr->first.second;
    if (x == 0 && y == 0) {
      itr++;
      continue;
    }
    ll c = itr->second;
    ll d = 0;
    if (m.find(mp(-y, x)) != end(m)) {
      d = m[mp(-y, x)];
    } else if (m.find(mp(y, -x)) != end(m)) {
      d = m[mp(y, -x)];
    }
    ans = ans * (modpow(2, c) + modpow(2, d) - 1) % mod;
    m[mp(x, y)] = 0;
    if (m.find(mp(-y, x)) != end(m)) {
      m[mp(-y, x)] = 0;
    } else if (m.find(mp(y, -x)) != end(m)) {
      d = m[mp(y, -x)] = 0;
    }
    itr++;
  }
  cout << (ans + cnt0 + mod - 1) % mod << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep3(i, a, b) for (ll i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<ll>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

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

ll mod = 1000000007;

ll modpow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2 == 0) {
    ll d = modpow(a, b / 2);
    return d * d % mod;
  }
  return a * modpow(a, b - 1) % mod;
}

ll divide(ll a, ll b) { return a * modpow(b, mod - 2) % mod; }

int main() {
  int n = in();
  vector<pll> a(n);
  rep(i, n) { a[i] = mp(lin(), lin()); }
  map<pll, int> m;
  rep(i, n) {
    ll x = a[i].first;
    ll y = a[i].second;
    ll g = gcd(abs(x), abs(y));
    x /= g;
    y /= g;
    if (x < 0) {
      x = -x;
      y = -y;
    }
    m[mp(x, y)]++;
  }
  ll ans = 1;
  auto itr = m.begin();
  while (itr != m.end()) {
    ll x = itr->first.first;
    ll y = itr->first.second;
    ll c = itr->second;
    ll d = 0;
    if (m.find(mp(-y, x)) != end(m)) {
      d = m[mp(-y, x)];
    } else if (m.find(mp(y, -x)) != end(m)) {
      d = m[mp(y, -x)];
    }
    // sbt+=c*d*modpow(2,n-c-d)%mod;
    ans *= (modpow(2, c) + modpow(2, d) - 1) % mod;
    itr++;
    // cout<<x<<" "<<y<<" "<<c<<" "<<d<<endl;
    // cout<<"x = "<<x<<"  y = "<<y<<"  c = "<<c<<"  d = "<<d<<"
    // "<<(modpow(2,c)+modpow(2,d)-1)%mod<<endl;
    m[mp(x, y)] = 0;
    if (m.find(mp(-y, x)) != end(m)) {
      m[mp(-y, x)] = 0;
    } else if (m.find(mp(y, -x)) != end(m)) {
      d = m[mp(y, -x)] = 0;
    }
  }
  // sbt = divide(sbt,2);
  // ans -= sbt;
  cout << (ans - 1) % mod << endl;
}