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

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
// using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;
const double PI = acos(-1.0);

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

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

const int MOD = 1000000007;

class mint {
  ll x;

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

  mint operator-(const mint &a) {
    mint res(*this);
    return res -= a;
  }

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

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  ll zero = 0;
  map<pair<ll, ll>, P> mp;
  for (int i = 0; i < n; i++) {
    ll x, y;
    cin >> x >> y;
    if (x == 0 && y == 0) {
      zero++;
      continue;
    }
    ll g = gcd(x, y);
    x /= g, y /= g;
    if (y < 0) {
      x = -x, y = -y;
    }
    if (y == 0 && x < 0) {
      x = -x, y = -y;
    }
    int rot90 = false;
    if (x <= 0)
      rot90 = true;
    if (rot90) {
      ll tmp = x;
      x = y, y = -tmp;
    }
    if (rot90)
      mp[{x, y}].first++;
    else
      mp[{x, y}].second++;
  }

  mint ans = 1;
  for (auto p : mp) {
    ll s = p.second.first;
    ll t = p.second.second;
    mint now = 1;              // どちらも選ばない
    now += mint(2).pow(s) - 1; // １匹以上
    now += mint(2).pow(t) - 1;
    ans *= now;
  }
  ans -= 1;
  ans += zero;
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:


#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
// using Graph = vector<vector<Edge>>;
using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

const int MOD = 1000000007;

class mint {
  ll x;

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

  mint operator-(const mint &a) {
    mint res(*this);
    return res -= a;
  }

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

  friend ostream &operator<<(ostream &os, const mint &m) {
    os << m.x;
    return os;
  }
};

const int MAX = 510000;
// const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N;
  cin >> N;
  vector<ll> A(N), B(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  COMinit();
  mint all = 0;
  for (int i = 1; i <= N; i++)
    all += COM(N, i);
  map<ll, ll> mp;
  ll rsize = 0;
  for (int i = 0; i < N; i++) {
    rsize += mp[-B[i] / A[i]];
    mp[A[i] / B[i]]++;
  }
  mint r = 0;
  r += COM(N, 1);
  for (int i = 2; i <= rsize; i++) {
    r += COM(N, i) - COM(rsize, i);
  }
  cout << r << endl;
  return 0;
}