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

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define N 200005
#define mod 1000000007LL

LL ans, a[N], b[N], _gcd, p, q;
int n, cur, T, i, r, vis[N], p0;
map<pair<LL, LL>, int> K;
pair<LL, LL> Q[N];
pair<pair<LL, LL>, int> label[N];

LL power(LL x, int y) {
  LL t = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      t = t * x % mod;
    x = x * x % mod;
  }
  return t;
}

int main(void) {
  // freopen("1.in","r",stdin);
  // freopen("1.out","w",stdout);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld%lld", &a[i], &b[i]);
    if (a[i] < 0 || !a[i] && b[i] < 0)
      a[i] = -a[i], b[i] = -b[i];
    if (!a[i] && !b[i]) {
      p0++;
      continue;
    }
    _gcd = __gcd(abs(a[i]), abs(b[i]));
    a[i] /= _gcd, b[i] /= _gcd;
    Q[i] = make_pair(a[i], b[i]);
  }
  sort(Q + 1, Q + n + 1);
  cur = T = 1;
  for (i = 2; i <= n; i++) {
    if (Q[i].first == Q[i - 1].first && Q[i].second == Q[i - 1].second)
      cur++;
    else
      label[T] = make_pair(Q[i - 1], cur), T++, cur = 1;
  }
  if (cur)
    label[T] = make_pair(Q[i - 1], cur);
  for (i = 1; i <= T; i++)
    K[label[i].first] = i;
  ans = 1;
  for (i = 1; i <= T; i++) {
    if (vis[i])
      continue;
    p = -label[i].first.second;
    q = label[i].first.first;
    if (!p && !q)
      continue;
    if (p < 0 || !p && q < 0)
      p = -p, q = -q;
    _gcd = __gcd(abs(p), abs(q));
    p /= _gcd, q /= _gcd;
    r = K[make_pair(p, q)];
    // printf("%d %d\\n",i,r);
    if (r) {
      ans = ans *
            (power(2LL, label[i].second) + power(2LL, label[r].second) - 1) %
            mod;
      if (ans < 0)
        ans += mod;
      vis[i] = vis[r] = 1;
    } else {
      ans = ans * power(2LL, label[i].second) % mod;
      vis[i] = 1;
    }
    // printf("%lld %d\\n",ans,T);
  }
  ans = (ans - 1) % mod;
  if (ans < 0)
    ans += mod;
  ans = (ans + p0) % mod;
  printf("%lld\\n", ans);
  // fclose(stdin);
  // fclose(stdout);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define N 200005
#define mod 1000000007

LL ans, a[N], b[N], _gcd, p, q;
int n, cur, T, i, r, vis[N];
map<pair<LL, LL>, int> K;
pair<LL, LL> Q[N];
pair<pair<LL, LL>, int> label[N];

LL power(LL x, int y) {
  LL t = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      t = t * x % mod;
    x = x * x % mod;
  }
  return t;
}

int main(void) {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld%lld", &a[i], &b[i]);
    _gcd = __gcd(a[i], b[i]);
    a[i] /= _gcd, b[i] /= _gcd;
    Q[i] = make_pair(a[i], b[i]);
  }
  sort(Q + 1, Q + n + 1);
  cur = T = 1;
  for (i = 2; i <= n; i++) {
    if (Q[i].first == Q[i - 1].first && Q[i].second == Q[i - 1].second)
      cur++;
    else
      label[T] = make_pair(Q[i - 1], cur), T++, cur = 1;
  }
  if (cur)
    label[T] = make_pair(Q[i - 1], cur);
  for (i = 1; i <= T; i++)
    K[label[i].first] = i;
  ans = 1;
  for (i = 1; i <= T; i++) {
    if (vis[i])
      continue;
    p = -label[i].first.second;
    q = label[i].first.first;
    _gcd = __gcd(p, q);
    p /= _gcd, q /= _gcd;
    r = K[make_pair(p, q)];
    if (!r) {
      p = label[i].first.second;
      q = -label[i].first.first;
      _gcd = __gcd(p, q);
      p /= _gcd, q /= _gcd;
      r = K[make_pair(p, q)];
    }
    if (r) {
      ans = ans * (power(2, label[i].second) + power(2, label[r].second) - 1) %
            mod;
      if (ans < 0)
        ans += mod;
      vis[i] = vis[r] = 1;
    } else {
      ans = ans * power(2, label[i].second) % mod;
      vis[i] = 1;
    }
    // printf("%lld %d\\n",ans,T);
  }
  ans = (ans - 1) % mod;
  if (ans < 0)
    ans += mod;
  printf("%lld\\n", ans);
  return 0;
}