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

#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)

using ll = long long int;

constexpr ll mod = 1e9 + 7;

class mint {
  ll num = 0;

public:
  mint(ll x) : num(x) {}

  mint operator+(const mint &rhs) { return (num + rhs.num) % mod; }
  mint operator*(const mint &rhs) { return num * rhs.num % mod; }
  mint operator-(const mint &rhs) { return (num - rhs.num + mod) % mod; }
  friend std::ostream &operator<<(std::ostream &os, const mint &rhs);
};

std::ostream &operator<<(std::ostream &os, const mint &rhs) {

  os << rhs.num;

  return os;
}

mint pow(mint x, int m) {

  mint re = 1;

  for (int i = 0; i <= 30; ++i) {
    if (m & (1 << i))
      re = re * x;
    x = x * x;
  }

  return re;
}

mint inv(mint x) { return pow(x, mod - 2); }

using P = std::pair<ll, ll>;

P normal(P p) { return (p.first >= 0) ? p : P(-p.first, -p.second); }

ll gcd(ll a, ll b) {
  if (a < b)
    std::swap(a, b);
  return (b == 0) ? a : gcd(b, a % b);
}

P red(P p) {

  ll g = gcd(std::abs(p.first), std::abs(p.second));

  // std::cerr << "DD" << g << std::endl;

  if (p.first == 0)
    return P(0, 1);
  else if (p.second == 0)
    return P(1, 0);
  else
    return normal(P(p.first / g, p.second / g));
}

P inv(P p) {

  if (p == P(1, 0))
    return P(0, 1);
  return normal(P(p.second, -p.first));
}

std::map<P, int> map;
std::vector<P> vec;
std::set<P> used;

int main() {

  int n;

  std::cin >> n;

  int cnt = 0;

  rep(i, n) {
    ll a, b;

    std::cin >> a >> b;

    if (a == 0 && b == 0) {
      ++cnt;
      continue;
    }

    P p = red(P(a, b));

    ++map[p];
    if (!map.count(inv(p)))
      map[inv(p)] = 0;
  }

  for (auto p : map) {
    if (!used.count(p.first)) {
      vec.emplace_back(p.second, map[inv(p.first)]);
      used.insert(p.first);
      used.insert(inv(p.first));
    }
  }

  mint ans = pow(2, n - cnt);

  // mint xx = 1;

  // std::cerr << "DEBUG" << std::endl;

  for (P p : vec) {
    // std::cerr << p.first << p.second << std::endl;
    ans = ans * (pow(2, p.first) + pow(2, p.second) - mint(1)) *
          inv(pow(2, p.first + p.second));
  }

  std::cout << (ans - 1 + cnt) << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)

using ll = long long int;

constexpr ll mod = 1e9 + 7;

class mint {
  ll num = 0;

public:
  mint(ll x) : num(x) {}

  mint operator+(const mint &rhs) { return (num + rhs.num) % mod; }
  mint operator*(const mint &rhs) { return num * rhs.num % mod; }
  mint operator-(const mint &rhs) { return (num - rhs.num + mod) % mod; }
  friend std::ostream &operator<<(std::ostream &os, const mint &rhs);
};

std::ostream &operator<<(std::ostream &os, const mint &rhs) {

  os << rhs.num;

  return os;
}

mint pow(mint x, int m) {

  mint re = 1;

  for (int i = 0; i <= 30; ++i) {
    if (m & (1 << i))
      re = re * x;
    x = x * x;
  }

  return re;
}

mint inv(mint x) { return pow(x, mod - 2); }

using P = std::pair<ll, ll>;

P normal(P p) { return (p.first >= 0) ? p : P(-p.first, -p.second); }

ll gcd(ll a, ll b) {
  if (a < b)
    std::swap(a, b);
  return (b == 0) ? a : gcd(b, a % b);
}

P red(P p) {

  ll g = gcd(std::abs(p.first), std::abs(p.second));

  // std::cerr << "DD" << g << std::endl;

  return normal(P(p.first / g, p.second / g));
}

P inv(P p) { return normal(P(p.second, -p.first)); }

std::map<P, int> map;
std::vector<P> vec;
std::set<P> used;

int main() {

  int n;

  std::cin >> n;

  rep(i, n) {
    ll a, b;

    std::cin >> a >> b;

    P p = red(P(a, b));

    ++map[p];
    if (!map.count(inv(p)))
      map[inv(p)] = 0;
  }

  for (auto p : map) {
    if (!used.count(p.first)) {
      vec.emplace_back(p.second, map[inv(p.first)]);
      used.insert(p.first);
      used.insert(inv(p.first));
    }
  }

  mint ans = pow(2, n);

  // mint xx = 1;

  // std::cerr << "DEBUG" << std::endl;

  for (P p : vec) {
    // std::cerr << p.first << p.second << std::endl;
    ans = ans * (pow(2, p.first) + pow(2, p.second) - mint(1)) *
          inv(pow(2, p.first + p.second));
  }

  std::cout << (ans - 1 + mod) << std::endl;

  return 0;
}