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

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E_Bullet.exe E_Bullet.cpp
Execute: ./E_Bullet
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std;

long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

long long power_mod(long long x, long long n, long long M) {
  long long int ret = 1;
  long long int by = x;
  while (n) {
    if (n & 1) {
      ret *= by;
      ret %= M;
    }
    by *= by;
    by %= M;
    n >>= 1;
  }
  return ret;
}

pair<long long, long long> make_orthogonal(long long a1, long long b1) {
  long long a, b;

  if (b1 < 0) {
    a = -a1;
    b = -b1;
  } else {
    a = a1;
    b = b1;
  }

  if (a > 0)
    return (make_pair(-b, a));
  if (a == 0)
    return (make_pair(1, 0));
  if (a < 0)
    return (make_pair(b, -a));

  return (make_pair(0, 0));
}

int main() {
  int n;
  long long a, b;
  long long g;
  long long mod = 1000000007ULL;
  map<pair<long long, long long>, pair<int, int>> sardine_slopes;
  // first = number parallel
  // second = number orthogonal

  sardine_slopes.clear();

  // Input and process the sardine slopes
  cin >> n;
  long long num_zero = 0;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    if (a == 0 && b == 0) {
      num_zero++;
    } else {
      g = abs(gcd(a, b));
      a /= g;
      b /= g;
      if (b < 0) {
        a *= -1;
        b *= -1;
      }
      if (sardine_slopes[make_orthogonal(a, b)].first > 0) {
        sardine_slopes[make_orthogonal(a, b)].second++;
      } else {
        if (b == 0 && a != 0) {
          sardine_slopes[make_pair(1, 0)].first++;
        }
        if (b > 0) {
          sardine_slopes[make_pair(a, b)].first++;
        }
        if (b < 0) {
          sardine_slopes[make_pair(-a, -b)].first++;
        }
      }
    }
  }

  long long result = 1, x, x1, x2;

  // cout << "Iterate through elements" << endl;
  for (map<pair<long long, long long>, pair<int, int>>::iterator it =
           sardine_slopes.begin();
       it != sardine_slopes.end(); it++) {
    if (it->second.first > 0) {
      x1 = power_mod(2, (it->second.first), mod);
      x2 = power_mod(2, (it->second.second), mod);
      x = (x1 + x2 - 1);
      x %= mod;
      result *= x;
      result %= mod;
    }
    /*
    cout << "(" << it->first.first << "," << it->first.second << ") -> " <<
    it->second.first << " " << it->second.second << endl;
    */
  }

  result += mod - 1;
  result %= mod;
  result += num_zero;
  result %= mod;

  // cout << "Result = ";
  cout << result << endl;

  return 0;
}


Error: 0

Fixed submission:

/* [Template].cpp
Example code to use when starting new code. This particular problem was problem
977A_Wrong_Subtraction from CodeForces

Compile: g++  -o E_Bullet.exe E_Bullet.cpp
Execute: ./E_Bullet
*/

#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
using namespace std;

long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}

long long power_mod(long long x, long long n, long long M) {
  long long int ret = 1;
  long long int by = x;
  while (n) {
    if (n & 1) {
      ret *= by;
      ret %= M;
    }
    by *= by;
    by %= M;
    n >>= 1;
  }
  return ret;
}

pair<long long, long long> make_orthogonal(long long a1, long long b1) {
  long long a, b;

  if (b1 < 0) {
    a = -a1;
    b = -b1;
  } else {
    a = a1;
    b = b1;
  }

  if (a > 0)
    return (make_pair(-b, a));
  if (a == 0)
    return (make_pair(1, 0));
  if (a < 0)
    return (make_pair(b, -a));

  return (make_pair(0, 0));
}

int main() {
  int n;
  long long a, b;
  long long g;
  long long mod = 1000000007ULL;
  map<pair<long long, long long>, pair<int, int>> sardine_slopes;
  // first = number parallel
  // second = number orthogonal

  sardine_slopes.clear();

  // Input and process the sardine slopes
  cin >> n;
  long long num_zero = 0;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    if (a == 0 && b == 0)
      num_zero++;
    g = abs(gcd(a, b));
    a /= g;
    b /= g;
    if (b < 0) {
      a *= -1;
      b *= -1;
    }
    if (sardine_slopes[make_orthogonal(a, b)].first > 0) {
      sardine_slopes[make_orthogonal(a, b)].second++;
    } else {
      if (b == 0 && a != 0) {
        sardine_slopes[make_pair(1, 0)].first++;
      }
      if (b > 0) {
        sardine_slopes[make_pair(a, b)].first++;
      }
      if (b < 0) {
        sardine_slopes[make_pair(-a, -b)].first++;
      }
    }
  }

  long long result = 1, x, x1, x2;

  // cout << "Iterate through elements" << endl;
  for (map<pair<long long, long long>, pair<int, int>>::iterator it =
           sardine_slopes.begin();
       it != sardine_slopes.end(); it++) {
    if (it->second.first > 0) {
      x1 = power_mod(2, (it->second.first), mod);
      x2 = power_mod(2, (it->second.second), mod);
      x = (x1 + x2 - 1);
      x %= mod;
      result *= x;
      result %= mod;
    }
    /*
    cout << "(" << it->first.first << "," << it->first.second << ") -> " <<
    it->second.first << " " << it->second.second << endl;
    */
  }

  result += mod - 1;
  result %= mod;
  result += num_zero;
  result %= mod;

  // cout << "Result = ";
  cout << result << endl;

  return 0;
}