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

#include <algorithm>
#include <boost/rational.hpp>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

const ll mod = 1000000007;

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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  std::vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

ll N;
ll A[200010], B[200010];
std::map<P, ll> mp;
std::map<ll, ll> mp0;
std::set<P> uniq;
mint two[200010];

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

P ortho(const P &p) {
  ll a = p.first, b = p.second;
  ll p1 = b, p2 = -a;

  if (p2 < 0) {
    p1 *= -1;
    p2 *= -1;
  }

  return P(p1, p2);
}

ll cnt[2];
ll cnt0;

int main() {
  std::cin >> N;

  rep(i, N) std::cin >> A[i] >> B[i];

  mint ans = 1;

  rep(i, N) {
    if (!A[i] && !B[i]) {
      cnt0 += 1;

      continue;
    }

    if (!A[i]) {
      cnt[0] += 1;

      continue;
    }

    if (!B[i]) {
      cnt[1] += 1;

      continue;
    }

    ll g = gcd(llabs(A[i]), llabs(B[i]));
    A[i] /= g;
    B[i] /= g;

    if (B[i] < 0) {
      ++mp[P(-A[i], -B[i])];
    } else
      ++mp[P(A[i], B[i])];
  }

  two[0] = 1;

  rep(i, 200000) two[i + 1] = two[i] * 2;

  itr(it, mp) {
    ll a = (it->first).first, b = (it->first).second;

    if (!uniq.count(it->first) && !uniq.count(ortho(it->first)))
      uniq.emplace(it->first);
  }

  for (auto rat : uniq) {
    ll a = rat.first, b = rat.second;

    // std::cout << rat << ' ' << ortho(rat) << std::endl;

    // std::cout << mp[rat] << ' ' << mp[ortho(rat)] << std::endl;

    if (!mp.count(ortho(rat)))
      ans *= two[mp[rat]];
    else
      ans *= two[mp[rat]] + two[mp[ortho(rat)]] - 1;
  }

  ans *= two[cnt[0]] + two[cnt[1]] - 1;

  ans += mint(cnt0);

  ans -= 1;

  std::cout << ans.x << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <boost/rational.hpp>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

const ll mod = 1000000007;

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 {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= 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 {
    mint res(*this);
    return res /= a;
  }
};

struct combination {
  std::vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

ll N;
ll A[200010], B[200010];
std::map<P, ll> mp;
std::map<ll, ll> mp0;
std::set<P> uniq;
mint two[200010];

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

P ortho(const P &p) {
  ll a = p.first, b = p.second;
  ll p1 = -b, p2 = a;

  if (p2 < 0)
    p1 *= -1, p2 *= -1;

  return P(p1, p2);
}

int main() {
  std::cin >> N;

  rep(i, N) std::cin >> A[i] >> B[i];

  mint ans = 1;

  rep(i, N) {
    bool ng = false;

    if (A[i] * B[i] < 0)
      ng = true;

    A[i] = llabs(A[i]);
    B[i] = llabs(B[i]);
    ll g = gcd(llabs(A[i]), llabs(B[i]));
    ++mp[P((ng ? -1 : 1) * A[i] / g, B[i] / g)];
  }

  two[0] = 1;

  rep(i, N + 5) two[i + 1] = two[i] * 2;

  itr(it, mp) {
    ll a = (it->first).first, b = (it->first).second;

    if (!uniq.count(it->first) && !uniq.count(ortho(it->first)))
      uniq.emplace(it->first);
  }

  for (auto rat : uniq) {
    ll a = rat.first, b = rat.second;

    if (!a && !b)
      continue;

    // std::cout << rat << ' ' << ortho(rat) << std::endl;

    // std::cout << mp[rat] << ' ' << mp[ortho(rat)] << std::endl;

    ans *= mint(1) + two[mp[rat]] - 1 + two[mp[ortho(rat)]] - 1;
  }

  std::cout << ans.x - 1 << std::endl;

  return 0;
}