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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18
const long MOD = 1000000007;

typedef std::pair<long, long> P;

long n;
std::map<P, long> mp;
std::map<P, P> mp2;

long power(long a, long b, const long MOD) {
  a %= MOD;
  if (b == 0) {
    return 1l;
  } else if (b % 2 == 1) {
    return a * power(a, b - 1, MOD) % MOD;
  } else {
    long r = power(a, b / 2, MOD);
    return r * r % MOD;
  }
}
long gcd(long num1, long num2) {
  if (num1 < 0) {
    num1 = -num1;
  }
  if (num2 < 0) {
    num2 = -num2;
  }
  if (num2 > num1) {
    std::swap(num1, num2);
  }
  if (num2 == 0) {
    return num1;
  } else {
    return gcd(num2, num1 % num2);
  }
}

P modify(P vec) {
  long a = vec.first;
  long b = vec.second;
  if (a == 0) {
    if (b != 0) {
      return P(0, 1);
    } else {
      return P(0, 0);
    }
  } else if (b == 0) {
    return P(1, 0);
  }
  long g = gcd(a, b);
  a /= g;
  b /= g;
  if (b == 0) {
    if (a < 0) {
      a = -a;
    }
  } else if (b < 0) {
    a = -a;
    b = -b;
  }
  return P(a, b);
}

bool is_quad(P v) {
  if (v == P(0, 0)) {
    return true;
  }
  if (v.first >= 0 && v.second > 0) {
    return true;
  }
  return false;
}

P vertical(P v) { return modify(P(v.second, -v.first)); }

long mod(long x, long m) { return (x % m + m) % m; }

int main() {

  scanf("%ld", &n);

  for (int i = 0; i < n; i++) {
    long a, b;
    scanf("%ld%ld", &a, &b);
    mp[modify(P(a, b))]++;
  }
  /*
  for(auto it = mp.begin(); it != mp.end(); it++){
      printf("(%ld, %ld), %ld\\n", it->first.first, it->first.second,
  it->second);
  }
  //*/
  for (auto it = mp.begin(); it != mp.end(); it++) {
    P v = it->first;
    long cnt = it->second;
    if (is_quad(v)) {
      mp2[v].first = cnt;
    } else {
      mp2[vertical(v)].second = cnt;
    }
  }
  /*
  for(auto it = mp2.begin(); it != mp2.end(); it++){
      P a = it->first;
      P b = it->second;
      printf("(%ld, %ld), %ld %ld\\n", a.first, a.second, b.first, b.second);
  }
  //*/
  long ans = 1;
  long zero = 0;
  for (auto it = mp2.begin(); it != mp2.end(); it++) {
    if (P(0, 0) == it->first) {
      zero = it->second.first;
      // printf("zero %ld\\n", zero);
      continue;
    }
    long x = it->second.first;
    long y = it->second.second;
    long res = (power(2, x, MOD) + power(2, y, MOD) - 1) % MOD;
    if (res < 0) {
      res += MOD;
    }
    // printf("  %ld\\n", res);
    ans *= res;
    ans %= MOD;
  }
  printf("%ld\\n", mod(ans - 1 + zero, MOD));
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18
const long MOD = 1000000007;

typedef std::pair<long, long> P;

long n;
std::map<P, long> mp;
std::map<P, P> mp2;

long power(long a, long b, long MOD) {
  if (b == 0) {
    return 1l;
  } else if (b % 2 == 1) {
    return a * power(a, b - 1, MOD) % MOD;
  } else {
    long r = power(a, b / 2, MOD);
    return r * r % MOD;
  }
}
long gcd(long num1, long num2) {
  if (num1 < 0) {
    num1 = -num1;
  }
  if (num2 < 0) {
    num2 = -num2;
  }
  if (num2 > num1) {
    std::swap(num1, num2);
  }
  if (num2 == 0) {
    return num1;
  } else {
    return gcd(num2, num1 % num2);
  }
}

P modify(P vec) {
  long a = vec.first;
  long b = vec.second;
  long g = gcd(a, b);
  a /= g;
  b /= g;
  if (b == 0) {
    if (a < 0) {
      a = -a;
    }
  } else if (b < 0) {
    a = -a;
    b = -b;
  }
  return P(a, b);
}

bool is_quad(P v) {
  if (v.first >= 0 && v.second > 0) {
    return true;
  }
  return false;
}

P vertical(P v) { return modify(P(v.second, -v.first)); }

int main() {
  scanf("%ld", &n);

  for (int i = 0; i < n; i++) {
    long a, b;
    scanf("%ld%ld", &a, &b);
    mp[modify(P(a, b))]++;
  }
  /*
  for(auto it = mp.begin(); it != mp.end(); it++){
      printf("(%ld, %ld), %ld\\n", it->first.first, it->first.second,
  it->second);
  }
  //*/
  long ans = 1;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    P v = it->first;
    long cnt = it->second;
    if (is_quad(v)) {
      mp2[v].first = cnt;
    } else {
      mp2[vertical(v)].second = cnt;
    }
  }
  /*
  for(auto it = mp2.begin(); it != mp2.end(); it++){
      P a = it->first;
      P b = it->second;
      printf("(%ld, %ld), %ld %ld\\n", a.first, a.second, b.first, b.second);
  }
  //*/

  for (auto it = mp2.begin(); it != mp2.end(); it++) {
    long x = it->second.first;
    long y = it->second.second;
    ans *= (power(2, x, MOD) + power(2, y, MOD) - 1) % MOD;
    ans %= MOD;
  }
  /*
  for(auto it = mp.begin(); it != mp.end(); it++){
      long x = it->second;
      if(x == -1){
          continue;
      }
      P v = it->first;
      long y = mp[modify(P(v.second, -v.first))];
      ans *= (power(2, x, MOD) + power(2, y, MOD) + 1) % MOD;
      ans %= MOD;
      mp[modify(P(v.second, -v.first))] = -1;
  }
  //*/
  printf("%ld\\n", ans - 1);
}