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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

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

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

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

int main() {
  ll N;
  cin >> N;
  vector<P> a(N);
  rep(i, N) cin >> a[i].fi >> a[i].sc;
  SORT(a);

  // 最小公倍数でわる
  rep(i, N) {
    if (a[i].fi == 0 and a[i].sc == 0)
      continue;
    ll tmp = gcd(abs(a[i].fi), abs(a[i].sc));
    a[i].fi /= tmp;
    a[i].sc /= tmp;
  }

  // map;
  ll zero = 0;
  map<P, ll> map;
  rep(i, N) {
    if (a[i].fi == 0 and a[i].sc == 0) {
      zero++;
      continue;
    } else if (a[i].fi == 0 and a[i].sc != 0) {
      a[i].sc = -1;
    } else if (a[i].fi != 0 and a[i].sc == 0) {
      a[i].fi = 1;
    } else if (a[i].fi < 0 and a[i].sc < 0) {
      a[i].fi = abs(a[i].fi);
      a[i].sc = abs(a[i].sc);
    } else if (a[i].fi < 0 and a[i].sc > 0) {
      a[i].fi = -a[i].fi;
      a[i].sc = -a[i].sc;
      // ll tmp = a[i].fi; a[i].fi = a[i].sc; a[i].sc = tmp;
    }
    map[mp(a[i].fi, a[i].sc)]++;
  }

  /*
  for(auto p : map){
    cout << p.fi.fi << " " << p.fi.sc << " " << p.sc << endl;
  }
  */

  // 相方の個数を数える
  ll ans = 1;
  for (auto p : map) {
    auto a = -p.fi.sc;
    auto b = p.fi.fi;
    auto c = p.fi.sc;
    auto d = -p.fi.fi;
    if (map[mp(a, b)] > 0 or map[mp(c, d)] > 0) {
      ll cnt = modpow(2, p.sc, MOD) - 1 + modpow(2, map[mp(a, b)], MOD) - 1 +
               modpow(2, map[mp(c, d)], MOD) - 1;
      cnt++;
      cnt %= MOD;
      ans *= cnt;
      ans %= MOD;
      map.erase(mp(a, b));
      map.erase(mp(c, d));
    } else {
      ans *= modpow(2, p.sc, MOD);
      ans %= MOD;
      map.erase(mp(a, b));
      map.erase(mp(c, d));
    }
  }
  ans = ans + MOD - 1;
  ans %= MOD;
  ans += zero;
  ans %= MOD;
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (ll i = 0; i < x; i++)
#define repn(i, x) for (ll i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

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

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

// MAXの値に注意 !!!!!
long long fac[MAX], finv[MAX], inv[MAX];

// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

// テーブルを作る前処理
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() {
  COMinit();
  ll N;
  cin >> N;
  vector<P> a(N);
  rep(i, N) cin >> a[i].fi >> a[i].sc;
  SORT(a);

  // 最小公倍数でわる
  rep(i, N) {
    ll tmp = gcd(abs(a[i].fi), abs(a[i].sc));
    a[i].fi /= tmp;
    a[i].sc /= tmp;
  }

  // map;
  map<P, ll> map;
  rep(i, N) map[mp(a[i].fi, a[i].sc)]++;
  // 変更していくやつ
  /*
  map<P,ll> map1;
  rep(i,N) map1[mp(a[i].fi , a[i].sc)]++;
  */

  // 相方の個数を数える
  ll ans = 1;
  for (auto p : map) {
    ll a = -p.first.second;
    ll b = p.first.first;
    ll c = p.first.second;
    ll d = -p.first.first;
    if (map[mp(a, b)] > 0 or map[mp(c, d)] > 0) {
      ll cnt = (map[p.fi] + 1) * (map[mp(a, b)] + map[mp(c, d)] + 1) - 1;
      map[mp(a, b)] = 0;
      map[mp(c, d)] = 0;
      ans *= cnt;
      ans %= MOD;
    } else {
      ans *= map[p.fi] + 1;
      ans %= MOD;
    }
  }
  ans = ans + MOD - 1;
  ans %= MOD;
  cout << ans << endl;
}