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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double PI = 3.14159265358979323846;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)

#define m0(x) memset((x), 0, sizeof((x)))
#define m1(x) memset((x), -1, sizeof((x)))

// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define all(x) (x).begin(), (x).end()

// 略記
#define pb emplace_back // vectorヘの挿入
#define mp make_pair    // pairのコンストラクタ
#define F first         // pairの一つ目の要素
#define S second        // pairの二つ目の要素

#define BITCOUNT __builtin_popcount
#define BITCOUNT_LL(x) __builtin_popcountll(x)

#define perm(c)                                                                \\
  sort(all(c));                                                                \\
  for (bool c##p = 1; c##p;                                                    \\
       c##p = next_permutation(all(c))) // 順列 123 132 213 231 312 321

#define BIT(n) (1LL << (n))

#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif

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++(int) { return mint(*this) += mint(1); }
  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; }

// 入力高速化
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

signed main() {
  ll N;
  cin >> N;
  map<P, ll> m;

  mint ans = 1;
  int zeros = 0;

  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    ll div = gcd(a, b);
    if (a == 0 && b == 0) {
      zeros++;
      continue;
    }
    m[mp(a / div, b / div)]++;
  }

  for (auto p : m) {
    int x = p.F.F;
    int y = p.F.S;
    int count = p.S;

    if (count == 0)
      continue;

    int pairCount = 0;
    if (x == 0) {
      pairCount += (m[mp(1, 0)] + m[mp(-1, 0)]);
      m.erase(mp(1, 0)) + m.erase(mp(-1, 0));
    } else if (y == 0) {
      pairCount += (m[mp(0, 1)] + m[mp(0, -1)]);
      m.erase(mp(0, 1)) + m.erase(mp(0, -1));
    } else {
      pairCount += (m[mp(y, -x)] + m[mp(-y, x)]);
      m.erase(mp(y, -x)) + m.erase(mp(-y, x));
    }
    mint now = 1; // 何も選ばれない　→1
    now += mint(2).pow(count) - 1; // 何も選ばれないのはすでに足してある　→　-1
    now +=
        mint(2).pow(pairCount) - 1; // 何も選ばれないのはすでに足してある　→　-1
    ans *= now;
    PRINT(x)
    PRINT(y)
    PRINT(count)
    PRINT(pairCount)
    PRINT(ans)
  }

  cout << ans - 1 + zeros << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double PI = 3.14159265358979323846;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)

#define m0(x) memset((x), 0, sizeof((x)))
#define m1(x) memset((x), -1, sizeof((x)))

// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める
#define all(x) (x).begin(), (x).end()

// 略記
#define pb emplace_back // vectorヘの挿入
#define mp make_pair    // pairのコンストラクタ
#define F first         // pairの一つ目の要素
#define S second        // pairの二つ目の要素

#define BITCOUNT __builtin_popcount
#define BITCOUNT_LL(x) __builtin_popcountll(x)

#define perm(c)                                                                \\
  sort(all(c));                                                                \\
  for (bool c##p = 1; c##p;                                                    \\
       c##p = next_permutation(all(c))) // 順列 123 132 213 231 312 321

#define BIT(n) (1LL << (n))

#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
#else
#define PRINT(A)
#endif

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

// 入力高速化
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v)
    is >> in;
  return is;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

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

ll choice[2 * 100000 + 100];

signed main() {
  ll N;
  cin >> N;
  map<P, ll> m;

  vector<ll> A(N), B(N);

  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    ll div = gcd(a, b);
    m[mp(a / div, b / div)]++;
  }

  mint ans = 1;
  mint zeros = 0;
  for (auto p : m) {
    int x = p.F.F;
    int y = p.F.S;
    int count = p.S;

    if (x == 0 && y == 0) {
      zeros += 1;
      continue;
    }

    if (count == 0)
      continue;

    int pairCount = 0;
    if (x == 0) {
      pairCount += (m[mp(1, 0)] + m[mp(-1, 0)]);
      m.erase(mp(1, 0)) + m.erase(mp(-1, 0));
    } else if (y == 0) {
      pairCount += (m[mp(0, 1)] + m[mp(0, -1)]);
      m.erase(mp(0, 1)) + m.erase(mp(0, -1));
    } else {
      pairCount += (m[mp(y, -x)] + m[mp(-y, x)]);
      m.erase(mp(y, -x)) + m.erase(mp(-y, x));
    }
    mint now = 1; // 何も選ばれない　→1
    now += mint(2).pow(count) - 1; // 何も選ばれないのはすでに足してある　→　-1
    now +=
        mint(2).pow(pairCount) - 1; // 何も選ばれないのはすでに足してある　→　-1
    ans *= now;
    PRINT(x)
    PRINT(y)
    PRINT(count)
    PRINT(pairCount)
    PRINT(ans)
  }

  cout << ans - 1 + zeros << endl;
}