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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using st = string;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
st abc = "abcdefghijklmnopqrstuvwxyz";
st ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

ll pow_mod(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else if (y % 2 == 0) {
    ll t = pow_mod(x, y / 2);
    return t * t % MOD;
  } else {
    return pow_mod(x, y - 1) * x % MOD;
  }
}

int main() {
  int N;
  cin >> N;
  vl A(N), B(N);
  map<P, ll> p;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 || b == 0) {
      if (a != 0) {
        a = 1;
      } else if (b != 0) {
        b = 1;
      }
    } else {
      ll x = gcd(a, b);
      a /= x;
      b /= x;
      if (a < 0) {
        a *= -1;
        b *= -1;
      }
    }
    A[i] = a;
    B[i] = b;
    p[{a, b}]++;
  }
  ll ans = 1;
  ll Z = 0;
  for (auto t : p) {
    auto key = t.first;
    auto value = t.second;
    ll x0 = key.first, x1 = key.second;
    ll z;
    p[{x0, x1}] = 0;
    if (value <= 0) {
      continue;
    }
    if (!(x0 == x1 && x0 == 0)) {
      if (x0 * x1 != 0) {
        z = p[{abs(x1), -abs(x1) / x1 * x0}];
        p[{abs(x1), -abs(x1) / x1 * x0}] = 0;
      } else if (x0 == 0) {
        z = p[{1, 0}];
        p[{1, 0}] = 0;
      } else {
        z = p[{0, 1}];
        p[{0, 1}] = 0;
      }
      ans = ans * (pow_mod(2, z) + (ll)pow_mod(2, value) - 1) % MOD;
    } else {
      Z = value;
      p[{0, 0}] = 0;
    }
  }
  cout << (MOD + ans + Z - 1) % MOD << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using st = string;
using ch = char;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
#define ROF(i, a, b) for (ll i = a; i >= b; i--)
#define per(i, a) ROF(i, a, 0)
#define pb push_back
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
st abc = "abcdefghijklmnopqrstuvwxyz";
st ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};
struct edge2 {
  ll from, to, cost;
};

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

ll pow_mod(ll x, ll y) {
  if (y == 0) {
    return 1;
  } else if (y % 2 == 0) {
    ll t = pow_mod(x, y / 2);
    return t * t % MOD;
  } else {
    return pow_mod(x, y - 1) * x % MOD;
  }
}

int main() {
  int N;
  cin >> N;
  vl A(N), B(N);
  map<P, ll> p;
  rep(i, N) {
    ll a, b;
    cin >> a >> b;
    if (a == 0 || b == 0) {
      if (a != 0) {
        a = 1;
      } else if (b != 0) {
        b = 1;
      }
    } else {
      ll x = gcd(a, b);
      a /= x;
      b /= x;
      if (a < 0) {
        a *= -1;
        b *= -1;
      }
    }
    A[i] = a;
    B[i] = b;
    p[{a, b}]++;
  }
  ll ans = 1;
  ll Z = 0;
  for (auto t : p) {
    auto key = t.first;
    auto value = t.second;
    ll x0 = key.first, x1 = key.second;
    ll z;
    p[{x0, x1}] = 0;
    if (value <= 0) {
      continue;
    }
    if (!(x0 == x1 && x0 == 0)) {
      if (x0 != 0 || x1 != 0) {
        z = p[{abs(x1), -abs(x1) / x1 * x0}];
        p[{abs(x1), -abs(x1) / x1 * x0}] = 0;
      } else if (x0 == 0) {
        z = p[{1, 0}];
        p[{1, 0}] = 0;
      } else {
        z = p[{0, 1}];
        p[{0, 1}] = 0;
      }
      ans = ans * (pow_mod(2, z) + (ll)pow_mod(2, value) - 1) % MOD;
    } else {
      Z = value;
      p[{0, 0}] = 0;
    }
  }
  cout << (ans + Z - 1) % MOD << endl;
}