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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

map<P, LL> m1;

LL gcd(LL a, LL b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long int pow_mod(long long int p_a, long long int p_n,
                      long long int p_p = 1e9 + 7) {
  // p_a^p_n mod p_p
  long long int p_b = 1, p_t = 1;
  for (; p_b <= p_n; p_b <<= 1)
    ;
  for (p_b >>= 1; p_b > 0; p_b >>= 1) {
    p_t *= p_t;
    if (p_t >= p_p)
      p_t %= p_p;
    if (p_n & p_b)
      p_t *= p_a;
    if (p_t >= p_p)
      p_t %= p_p;
  }
  return p_t;
}

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c, d;
  LL s = 1, s2 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    if (a == 0 && b == 0) {
      s2++;
      continue;
    }
    c = gcd(a, b);
    a /= c, b /= c;
    if (a < 0 || (a == 0 && b < 0))
      a = -a, b = -b;
    m1[make_pair(a, b)]++;
  }
  for (auto &node : m1) {
    a = node.first.first, b = node.first.second;
    c = pow_mod(2, node.second) - 1;
    swap(a, b);
    b = -b;
    if (a < 0 || (a == 0 && b < 0))
      a = -a, b = -b;
    if (m1.find(make_pair(a, b)) != m1.end()) {
      d = m1[make_pair(a, b)];
      if (d == -1)
        continue;
    } else
      d = 0;
    c += pow_mod(2, d);
    c %= MAX;
    s *= c;
    s %= MAX;
    node.second = -1;
  }
  s--;
  s += s2;
  if (s < 0)
    s += MAX;
  s %= MAX;
  cout << s << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<LL, LL> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\\n'));
}

map<P, int> m1;

LL gcd(LL a, LL b) {
  if (a < 0)
    a = -a;
  if (b < 0)
    b = -b;
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

long long int pow_mod(long long int p_a, long long int p_n,
                      long long int p_p = 1e9 + 7) {
  // p_a^p_n mod p_p
  long long int p_b = 1, p_t = 1;
  for (; p_b <= p_n; p_b <<= 1)
    ;
  for (p_b >>= 1; p_b > 0; p_b >>= 1) {
    p_t *= p_t;
    if (p_t >= p_p)
      p_t %= p_p;
    if (p_n & p_b)
      p_t *= p_a;
    if (p_t >= p_p)
      p_t %= p_p;
  }
  return p_t;
}

int main() {
  int n, m;
  int i, j, k;
  LL a, b, c, d;
  LL s = 1;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    c = gcd(a, b);
    a /= c, b /= c;
    if (a < 0 || (a == 0 && b < 0))
      a = -a, b = -b;
    m1[make_pair(a, b)]++;
  }
  for (auto &node : m1) {
    a = node.first.first, b = node.first.second;
    if (a == 0 && b == 0)
      continue;
    c = pow_mod(2, node.second) - 1;
    swap(a, b);
    b = -b;
    if (a < 0 || (a == 0 && b < 0))
      a = -a, b = -b;
    if (m1.find(make_pair(a, b)) != m1.end()) {
      d = m1[make_pair(a, b)];
      if (d == -1)
        continue;
    } else
      d = 0;
    c += pow_mod(2, d);
    c %= MAX;
    s *= c;
    s %= MAX;
    node.second = -1;
  }
  s--;
  if (s < 0)
    s += MAX;
  cout << s << endl;
}