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

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
using ll = long long;
typedef pair<ll, ll> P;
typedef pair<P, ll> PP;
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;
}
const ll INF = 1e18;

// input
ll n;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  cin >> n;
  ll zero = 0;
  ll r = 0;
  ll l = 0;
  set<P> st;
  map<P, ll> sei, fu;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 && b == 0) {
      zero++;
      continue;
    }
    if (a == 0) {
      r++;
      continue;
    }
    if (b == 0) {
      l++;
      continue;
    }
    bool minus = false;
    if (a > 0 && b < 0)
      minus = true;
    if (a < 0 && b > 0)
      minus = true;
    a = abs(a);
    b = abs(b);
    ll g = __gcd(a, b);
    a /= g;
    b /= g;
    P p = {a, b};
    if (minus)
      p = {a, -b};
    if (minus) {
      fu[p]++;
    } else {
      st.insert(p);
      sei[p]++;
    }
  }
  mint ans = 0;
  ans += modpow(2, l);
  ans += modpow(2, r);
  ans -= 1;

  for (auto it = sei.begin(); it != sei.end(); it++) {
    ll x = it->second;
    P p = it->first;
    P opo = {p.second, -p.first};
    ll y = fu[opo];
    mint now = 0;
    now += modpow(2, x);
    now += modpow(2, y);
    now -= 1;
    ans *= now;
  }

  for (auto it = fu.begin(); it != fu.end(); it++) {
    ll x = it->second;
    if (x == 0)
      continue;
    P p = it->first;
    P opo = {-p.second, p.first};
    if (st.count(opo) > 0)
      continue;
    ans *= modpow(2, x);
  }

  ans += zero;
  cout << ans - 1 << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(a, n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<vector<ll>> Graph;
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;
}
const ll INF = 1e18;

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

const int mod = 1000000007;
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  ll n;
  cin >> n;
  map<P, int> mp;
  rep(i, n) {
    ll a, b;
    cin >> a >> b;
    ll d = gcd(abs(a), abs(b));
    a /= d;
    b /= d;
    if (a < 0) {
      a = -a;
      b = -b;
    }
    mp[make_pair(a, b)]++;
  }
  mint ans = 0;
  ans += modpow(2, n);
  ans -= 1;
  mint l = 1;
  ll non = 0;
  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    auto it = *itr;
    P p = it.first;
    if (p.second < 0) {
      non += it.second;
      continue;
    }
    ll f = it.second;
    ll x = p.first;
    ll y = p.second;
    ll s = mp[make_pair(y, -x)];
    if (s == 0) {
      non += f;
      continue;
    }
    mint now = (modpow(2, f) - 1) * (modpow(2, s) - 1);
    l *= now;
  }
  ans = modpow(2, non + 1);
  ans -= l;
  cout << ans << endl;
  return 0;
}