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

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  in(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  in(__VA_ARGS__)
#define ULL(...)                                                               \\
  ull __VA_ARGS__;                                                             \\
  in(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  in(__VA_ARGS__)
#define CHR(...)                                                               \\
  char __VA_ARGS__;                                                            \\
  in(__VA_ARGS__)
#define DBL(...)                                                               \\
  double __VA_ARGS__;                                                          \\
  in(__VA_ARGS__)
#define LD(...)                                                                \\
  ld __VA_ARGS__;                                                              \\
  in(__VA_ARGS__)
void in() {}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << h;
  if (sizeof...(t) > 0)
    cerr << " :";
  debug_out(t...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vec) {
  for (size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
ll ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
bool pairCompare(const pll firstElof, pll secondElof) {
  return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
// rep, vin, all, iteLoop//
// bitSearch, bitList, nod, gcdlcm, isPrime, primeFactorize, Npow, divisor,
// modinv, dfs, bfs, eratos// dijkstra, WarshallFloyd, BellmanFord, UnionFind,
// COM, digitDP ,RMQ//
ll i, j, k, l;
ll N, M, K, H, W, L, X, Y, Z, R, Q;
ll MOD = 1000000007;
ll INF = 1LL << 60;
ll ans = 0;
ll z = 0, o = 1;
vl flag, D;
vll path;
vl Ans;
//***********//
ll gcd(ll x, ll y) {
  ll r;
  if (x < y) {
    swap(x, y);
  }
  while (y > 0) {
    r = x % y;
    x = y;
    y = r;
  }
  return x;
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
ll Npow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return Npow(x * x % MOD, n / 2);
  else
    return x * Npow(x, n - 1) % MOD;
}
int main() {
  cin >> N;
  vl A(N), B(N);
  map<pll, ll> M;
  ll pre2 = N;
  ll zz = 0;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (A[i] == 0 && B[i] == 0) {
      zz++;
    } else {
      if (B[i] == 0) {
        M[mp(1, 0)]++;
      } else if (B[i] > 0) {
        ll D = gcd(abs(A[i]), abs(B[i]));
        M[mp(A[i] / D, B[i] / D)]++;
      } else {
        ll D = gcd(abs(A[i]), abs(B[i]));
        M[mp(-A[i] / D, -B[i] / D)]++;
      }
    }
  }
  ans = 1;

  for (auto x : M) {
    ll a = x.second;
    ll b = M[mp(-x.first.second, x.first.first)];
    if (a > 0 && b > 0) {
      ll pre = (Npow(2, a) + Npow(2, b) - 1) % MOD;
      (ans *= pre) %= MOD;
      pre2 -= a + b;
    }
  }
  pre2 -= zz;
  (ans *= Npow(2, pre2)) %= MOD;
  ans = (ans - 1 + MOD) % MOD;
  if (zz > 0)
    (ans += zz) %= MOD;
  cout << ans;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  in(__VA_ARGS__)
#define LL(...)                                                                \\
  ll __VA_ARGS__;                                                              \\
  in(__VA_ARGS__)
#define ULL(...)                                                               \\
  ull __VA_ARGS__;                                                             \\
  in(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  in(__VA_ARGS__)
#define CHR(...)                                                               \\
  char __VA_ARGS__;                                                            \\
  in(__VA_ARGS__)
#define DBL(...)                                                               \\
  double __VA_ARGS__;                                                          \\
  in(__VA_ARGS__)
#define LD(...)                                                                \\
  ld __VA_ARGS__;                                                              \\
  in(__VA_ARGS__)
void in() {}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
  cerr << " " << h;
  if (sizeof...(t) > 0)
    cerr << " :";
  debug_out(t...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> vec) {
  for (size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
ll ctoi(char c) {
  switch (c) {
  case '0':
    return 0;
  case '1':
    return 1;
  case '2':
    return 2;
  case '3':
    return 3;
  case '4':
    return 4;
  case '5':
    return 5;
  case '6':
    return 6;
  case '7':
    return 7;
  case '8':
    return 8;
  case '9':
    return 9;
  default:
    return 0;
  }
}
bool pairCompare(const pll firstElof, pll secondElof) {
  return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
// rep, vin, all, iteLoop//
// bitSearch, bitList, nod, gcdlcm, isPrime, primeFactorize, Npow, divisor,
// modinv, dfs, bfs, eratos// dijkstra, WarshallFloyd, BellmanFord, UnionFind,
// COM, digitDP ,RMQ//
ll i, j, k, l;
ll N, M, K, H, W, L, X, Y, Z, R, Q;
ll MOD = 1000000007;
ll INF = 1LL << 60;
ll ans = 0;
ll z = 0, o = 1;
vl flag, D;
vll path;
vl Ans;
//***********//
ll binarySearch(vl V, ll key) {
  ll left = -1; // 「index = 0」が条件を満たすこともあるので、初期値は -1
  ll right = V.size(); // 「index =
                       // a.size()-1」が条件を満たさないこともあるので、初期値は
                       // V.size()
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;
    if (V[mid] >= key)
      right = mid;
    else
      left = mid;
  }
  /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている
   */
  return right;
}
ll Npow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return Npow(x * x % MOD, n / 2);
  else
    return x * Npow(x, n - 1) % MOD;
}
map<ll, ll> primeFactorize(ll n, map<ll, ll> &PF) {
  while (n % 2 == 0) {
    PF[2]++;
    n /= 2;
  }
  for (ll p = 3; p * p <= n; p += 2) {
    while (n % p == 0) {
      PF[p]++;
      n /= p;
    }
  }
  if (n != 1) {
    PF[n]++;
  }
  return PF;
}
int main() {
  cin >> N;
  vl A(N), B(N);
  ll Az = 0, Bz = 0, allz = 0;
  vpll P, Q;
  for (i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    if (A[i] == 0 && B[i] == 0)
      allz++;
    else if (A[i] == 0)
      Az++;
    else if (B[i] == 0)
      Bz++;
    else if (A[i] > 0 && B[i] > 0) {
      P.push_back(mp(A[i], B[i]));
    } else if (A[i] > 0 && B[i] < 0) {
      Q.push_back(mp(A[i], -B[i]));
    } else if (A[i] < 0 && B[i] > 0) {
      Q.push_back(mp(-A[i], B[i]));
    } else if (A[i] < 0 && B[i] < 0) {
      P.push_back(mp(-A[i], -B[i]));
    }
  }
  ans = Npow(2, N) - 1;
  // 0を含む場合の処理
  ll Z = Az * Bz + Az * allz + Bz * allz;
  ans = ((ans - (Z * Npow(2, N - 2)) % MOD + MOD) % MOD + MOD) % MOD;
  // debug(ans);
  // それ以外
  map<map<ll, ll>, ll> Ans1;
  map<map<ll, ll>, ll> Ans2;
  for (i = 0; i < P.size(); i++) {
    map<ll, ll> Pa, Pb, Pc;
    primeFactorize(P[i].first, Pa);
    primeFactorize(P[i].second, Pb);
    Pc = Pb;
    for (auto x : Pa) {
      Pc[x.first] -= x.second;
    }
    Ans1[Pc]++;
  }
  for (i = 0; i < Q.size(); i++) {
    map<ll, ll> Qa, Qb, Qc;
    primeFactorize(Q[i].first, Qa);
    primeFactorize(Q[i].second, Qb);
    Qc = Qa;
    for (auto x : Qb) {
      Qc[x.first] -= x.second;
    }
    Ans2[Qc]++;
  }
  ll ans = 1;
  ll pre2 = N;
  for (auto x : Ans1) {
    ll pre = x.second * Ans2[x.first];
    // debug(x.second, Ans2[x.first],pre);
    ll a = x.second;
    ll b = Ans2[x.first];
    if (a > 0 && b > 0) {
      ans =
          (ans * (Npow(2, a + b) - (Npow(2, a) - 1) * (Npow(2, b) - 1)) % MOD) %
          MOD;
      // debug(ans);
      pre2 -= a + b;
    }
  }
  (ans *= Npow(2, pre2)) %= MOD;
  ans--;
  cout << ans;
}