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

#include <bits/stdc++.h>

using namespace std;

long long mod = 1e9 + 7;
const int N = 2e5 + 7;

int n;
long long fac[N];
long long sol;
vector<int> adj[N];
bool vis[N];

long long faspow(long long x, long long y) {
  if (!y)
    return 1ll;
  long long ret = faspow(x, y / 2);
  ret = (1ll * ret * ret) % mod;
  if (y % 2) {
    ret = (1ll * ret * x) % mod;
  }
  return ret;
}

void norm(long long &x, long long &y) {
  if (x < 0 && y < 0) {
    x = -x;
    y = -y;
  }
  if (x < 0) {
    x = -x;
    y = -y;
  }
  long long gc = __gcd(abs(x), abs(y));
  x /= gc;
  y /= gc;
}

int cnt = 1;
map<pair<int, int>, int> mps;
long long w[N];

int comp(pair<int, int> pr, bool tole) {
  if (mps[pr]) {
    w[mps[pr]] += tole;
    return mps[pr];
  }
  if (!tole)
    return 0;
  w[cnt]++;
  mps[pr] = cnt++;
  return cnt - 1;
}

void mul(long long &x, long long y) { x = (1ll * (x % mod) * (y % mod)) % mod; }

void add(long long &x, long long y) { x = (x + y + mod) % mod; }

void addE(int u, int v) {
  if (v && u != v) {
    adj[u].push_back(v);
  }
}

pair<long long, long long> cop(pair<long long, long long> x) {
  swap(x.first, x.second);
  if (x.first < 0)
    x.first *= -1;
  else
    x.second *= -1;
  return x;
}

long long dfs(int x) {
  long long ret = faspow(2, w[x]) - 1;
  vis[x] = 1;
  for (auto u : adj[x]) {
    if (vis[u])
      continue;
    add(ret, dfs(u));
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fac[0] = 1ll;
  for (int i = 1; i < N; i++) {
    fac[i] = 1ll * i;
    mul(fac[i], fac[i - 1]);
  }
  vector<pair<long long, long long>> vs, v;

  cin >> n;
  map<pair<long long, long long>, int> mp;
  long long cof1 = 0ll, cof2 = 0ll;
  long long extra = 0;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (a == 0 || b == 0) {
      if (a == 0 && b == 0)
        extra++;
      else if (a == 0)
        cof1++;
      else
        cof2++;
      continue;
    }
    norm(a, b);
    comp({a, b}, 1);
    v.push_back({a, b});
  }

  v.resize(unique(v.begin(), v.end()) - v.begin());

  for (auto u : v) {
    pair<long long, long long> pru = u;
    int x = comp(u, 0);

    addE(x, comp(cop(pru), 0));
  }

  long long ans = 1ll;
  for (int i = 1; i < cnt; i++) {
    if (!vis[i]) {
      mul(ans, 1 + dfs(i));
    }
  }
  mul(ans, faspow(2, cof1) + faspow(2, cof2) - 1);
  cout << (ans + extra + mod - 1) % mod;
  ;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

long long mod = 1e9 + 7;

int n;

const int N = 2e5 + 7;

long long faspow(long long x, long long y) {
  if (!y)
    return 1ll;
  long long ret = faspow(x, y / 2);
  ret = (1ll * ret * ret) % mod;
  if (y % 2) {
    ret = (1ll * ret * x) % mod;
  }
  return ret;
}

long long fac[N];

long long res(long long x) { return faspow(x, mod - 2); }

long long ncr(long long x, long long y) {
  long long num = fac[x];
  long long den = 1ll * (res(y)) * res(x - y) % mod;
  return num * den % mod;
}

long long sol;

void norm(long long &x, long long &y) {
  long long gc = __gcd(abs(x), abs(y));
  x /= gc;
  y /= gc;
}

int cnt = 1;
map<pair<int, int>, int> mps;
int w[N];

int comp(pair<int, int> pr, bool tole) {
  if (mps[pr]) {
    w[mps[pr]] += tole;
    return mps[pr];
  }
  if (!tole)
    return 0;
  w[cnt]++;
  mps[pr] = cnt++;
  return cnt - 1;
}

vector<int> adj[N];

long long mul(long long &x, long long y) { x = (1ll * x * y) % mod; }

long long add(long long &x, long long y) { x = (x + y) % mod; }

void addE(int u, int v) {
  if (v) {
    adj[u].push_back(v);
  }
}

bool vis[N];

long long dfs(int x) {
  long long ret = faspow(2, w[x]) - 1;
  vis[x] = 1;
  for (auto u : adj[x]) {
    if (vis[u])
      continue;
    add(ret, dfs(u));
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  fac[0] = 1ll;
  for (int i = 1; i < N; i++) {
    fac[i] = 1ll * i * fac[i - 1];
  }
  vector<pair<long long, long long>> vs, v;

  cin >> n;
  map<pair<long long, long long>, int> mp;

  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (a == 0 || b == 0) {
      vs.push_back({a, b});
      continue;
    }
    norm(a, b);
    comp({a, b}, 1);
    v.push_back({a, b});
  }

  v.resize(unique(v.begin(), v.end()) - v.begin());

  for (auto u : v) {
    pair<long long, long long> pru = u;
    int x = comp(u, 0);
    addE(x, comp({-pru.second, pru.first}, 0));
    addE(x, comp({pru.second, -pru.first}, 0));
  }

  long long ans = 1ll;
  for (int i = 1; i < cnt; i++) {
    if (!vis[i]) {
      mul(ans, 1 + dfs(i));
    }
  }

  cout << (ans + mod - 1) % mod;
  ;
  return 0;
}