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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

void print(vector<pii> X) {
  for (int i = 0; i < X.size(); i++) {
    cout << X[i].first << " " << X[i].second << endl;
  }
  return;
}

ll gcd(ll x, ll y) {
  // cout<<x<<" "<<y<<endl;
  if (y == 0 || y == x) {
    return x;
  }
  if (x == 0) {
    return y;
  }
  if (y > x) {
    ll r = x;
    x = y;
    y = r;
  }
  return gcd(y, x % y);
}

int sigma(pll u) {
  if (u.first > 0 && u.second > 0) {
    return 1;
  }
  if (u.first < 0 && u.second < 0) {
    return 1;
  }
  return -1;
}

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  a = a % liek;
  if (b == 1) {
    return a;
  }
  ll x = laip((a * a) % liek, b / 2, liek);
  if (b % 2 == 0) {
    return x;
  }
  return (a * x) % liek;
}

ll solve(vector<pii> &X, ll liek) {
  ll ats = 1;
  for (int i = 0; i < (int)X.size(); i++) {
    pii u = X[i];
    ll v;
    if (u.first == 0) {
      v = laip(2, u.second, liek);
    }
    if (u.second == 0) {
      v = laip(2, u.first, liek);
    }
    if (u.first > 0 && u.second > 0) {
      v = laip(2, u.second, liek) + laip(2, u.first, liek) - 1;
    }
    v = (v + liek) % liek;
    ats = ats * v;
    ats = ats % liek;
  }
  return ats;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int w = 0;
  vector<pll> A;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a != 0 || b != 0) {
      ll x = gcd(abs(a), abs(b));
      if (b < 0) {
        a = -a;
        b = -b;
      }
      A.push_back({a / x, b / x});
    } else {
      w++;
    }
  }
  map<pll, pii> M;
  for (int i = 0; i < (int)A.size(); i++) {
    pll u = A[i];
    if (u.first != 0 && u.second != 0) {
      int a = sigma(u);
      if (a == 1) {
        // cout<<"Pirmas "<<u.first<<" "<<u.second<<endl;
        if (M.find(u) != M.end()) {
          M[u].first++;
        } else {
          M[u] = {1, 0};
        }
      } else {
        pll v = {abs(u.second), abs(u.first)};
        // cout<<"Antras "<<v.first<<" "<<v.second<<endl;
        if (M.find(v) != M.end()) {
          M[v].second++;
        } else {
          M[v] = {0, 1};
        }
      }
    } else {
      pll s = {0, 0};
      if (u.first == 0) {
        if (M.find(s) != M.end()) {
          M[s].first++;
        } else {
          M[s] = {1, 0};
        }
      } else {
        if (M.find(s) != M.end()) {
          M[s].second++;
        } else {
          M[s] = {0, 1};
        }
      }
    }
  }
  vector<pii> X;
  for (auto f : M) {
    X.push_back(f.second);
  }
  // print(X);
  ll liek = 1000000007;
  ll ats = (solve(X, liek) - 1 + liek + w) % liek;
  cout << ats << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

void print(vector<pii> X) {
  for (int i = 0; i < X.size(); i++) {
    cout << X[i].first << " " << X[i].second << endl;
  }
  return;
}

ll gcd(ll x, ll y) {
  // cout<<x<<" "<<y<<endl;
  if (y == 0 || y == x) {
    return x;
  }
  if (y > x) {
    ll r = x;
    x = y;
    y = r;
  }
  return gcd(y, x % y);
}

int sigma(pll u) {
  if (u.first > 0 && u.second > 0) {
    return 1;
  }
  return -1;
}

ll laip(ll a, ll b, ll liek) {
  if (b == 0) {
    return 1;
  }
  a = a % liek;
  if (b == 1) {
    return a;
  }
  ll x = laip(a * a, b / 2, liek);
  if (b % 2 == 0) {
    return x;
  }
  return (a * x) % liek;
}

ll solve(vector<pii> &X, ll liek) {
  int n = X.size();
  if (n == 0) {
    return 1;
  }
  pii u = X[n - 1];
  X.pop_back();
  ll y = solve(X, liek) % liek;
  if (u.first == 0) {
    return (laip(2, u.second, liek) * y) % liek;
  }
  if (u.second == 0) {
    return (laip(2, u.first, liek) * y) % liek;
  }
  return ((laip(2, u.second, liek) + laip(2, u.first, liek) - 1) * y) % liek;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pll> A;
  for (int i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    if (a != 0 || b != 0) {
      ll x = gcd(abs(a), abs(b));
      if (b < 0) {
        a = -a;
        b = -b;
      }
      A.push_back({a / x, b / x});
    }
  }
  map<pll, pii> M;
  for (int i = 0; i < (int)A.size(); i++) {
    pll u = A[i];
    if (u.first != 0 && u.second != 0) {
      int a = sigma(u);
      if (a == 1) {
        // cout<<"Pirmas "<<u.first<<" "<<u.second<<endl;
        if (M.find(u) != M.end()) {
          M[u].first++;
        } else {
          M[u] = {1, 0};
        }
      } else {
        pll v = {abs(u.second), abs(u.first)};
        // cout<<"Antras "<<v.first<<" "<<v.second<<endl;
        if (M.find(v) != M.end()) {
          M[v].second++;
        } else {
          M[v] = {0, 1};
        }
      }
    } else {
      if (u.first == 0) {
        pll s = {0, abs(u.second)};
        if (M.find(s) != M.end()) {
          M[s].first++;
        } else {
          M[s] = {1, 0};
        }
      } else {
        pll s = {abs(u.first), 0};
        if (M.find(s) != M.end()) {
          M[s].second++;
        } else {
          M[s] = {0, 1};
        }
      }
    }
  }
  vector<pii> X;
  for (auto f : M) {
    X.push_back(f.second);
  }
  // print(X);
  ll liek = 1000000007;
  cout << solve(X, liek) - 1 << endl;
  return 0;
}