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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define hii cout << "hii" << endl
#define int ll
#define endl '\\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 3e5 + 5;
const int MAXN = 3e5 + 5;
const int M = 3e5;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const long double co = 0.0174533;
const int INF = 1e15;
const int LG = 21;
const long double PI = acos(-1);

int n;
int arr[N];

vector<int> g[N];
int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> queries;
  map<pair<int, int>, int> neg;
  map<pair<int, int>, int> pos;
  map<pair<int, int>, int> zero;
  set<pair<int, int>> s;
  set<pair<int, int>> m;
  int z = 0;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    if (a == 0 and b == 0) {
      z++;
      continue;
    }
    if (a == 0) {
      zero[{0, 1}]++;
      continue;
    } else if (b == 0) {
      zero[{1, 0}]++;
      continue;
    }
    int tempa = abs(a);
    int tempb = abs(b);
    int g = __gcd(tempa, tempb);
    tempa /= g;
    tempb /= g;
    if (a < 0)
      tempa *= -1;
    if (b < 0)
      tempb *= -1;
    a = tempa;
    b = tempb;
    queries.push_back({a, b});
    if ((a < 0 and b > 0) or (a > 0 and b < 0)) {
      m.insert({abs(a), abs(b)});
      neg[{abs(a), abs(b)}]++;
    } else {
      s.insert({abs(a), abs(b)});
      pos[{abs(a), abs(b)}]++;
    }
  }
  vector<pair<int, int>> comp;
  for (auto x : s) {
    comp.push_back({pos[{x.first, x.second}], neg[{x.second, x.first}]});
  }
  for (auto x : m) {
    if (pos.count({x.second, x.first}))
      continue;
    comp.push_back({0LL, neg[{x.first, x.second}]});
  }
  comp.push_back({zero[{0, 1}], zero[{1, 0}]});
  int ret = 1;
  for (auto x : comp) {
    trace(x);
    int way1 = (pow(2, x.first, mod) - 1 + mod) % mod;
    int way2 = (pow(2, x.second, mod) - 1 + mod) % mod;
    way1 += 1;
    way1 += way2;
    way1 %= mod;
    ret *= way1;
    ret %= mod;
  }
  if (z) {
    ret += (z);
  }
  ret -= 1;
  ret += mod;
  ret %= mod;
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}

Error: 0
x : 1 1
x : 0 1


Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define hii cout << "hii" << endl
#define int ll
#define endl '\\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 3e5 + 5;
const int MAXN = 3e5 + 5;
const int M = 3e5;
const int mod = 1e9 + 7;
const int MOD = 1e9 + 7;
const long double co = 0.0174533;
const int INF = 1e15;
const int LG = 21;
const long double PI = acos(-1);

int n;
int arr[N];

vector<int> g[N];
int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> queries;
  map<pair<int, int>, int> neg;
  map<pair<int, int>, int> pos;
  map<pair<int, int>, int> zero;
  set<pair<int, int>> s;
  set<pair<int, int>> m;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    int tempa = abs(a);
    int tempb = abs(b);
    int g = __gcd(tempa, tempb);
    tempa /= g;
    tempb /= g;
    if (a < 0)
      tempa *= -1;
    if (b < 0)
      tempb *= -1;
    a = tempa;
    b = tempb;
    queries.push_back({a, b});
    if (a == 0 or b == 0) {
      zero[{0, 0}]++;
    } else if ((a < 0 and b > 0) or (a > 0 and b < 0)) {
      m.insert({abs(a), abs(b)});
      neg[{abs(a), abs(b)}]++;
    } else {
      s.insert({abs(a), abs(b)});
      pos[{abs(a), abs(b)}]++;
    }
  }
  vector<pair<int, int>> comp;
  for (auto x : s) {
    comp.push_back({pos[{x.first, x.second}], neg[{x.second, x.first}]});
  }
  for (auto x : m) {
    if (pos.count({x.second, x.first}))
      continue;
    comp.push_back({0LL, neg[{x.first, x.second}]});
  }
  int ret = 1;
  for (auto x : comp) {
    trace(x);
    ret *= (((pow(2, x.first, mod) + pow(2, x.second, mod) - 1 + mod)) % mod);
    ret %= mod;
  }
  ret *= (zero[{0, 0}] + 1);
  ret %= mod;
  ret -= 1;
  ret += mod;
  ret %= mod;
  cout << ret << endl;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}