Problem p02679 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  map<pll, ll> cnta, cntb;
  ll sum = n;
  ll ans = 0;
  ll z = 0;
  ll za = 0;
  ll zb = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] * b[i] != 0) {
      ll tmp = gcd(abs(a[i]), abs(b[i]));
      a[i] /= tmp;
      b[i] /= tmp;
      if (a[i] * b[i] > 0) {
        a[i] = abs(a[i]);
        b[i] = abs(b[i]);
      } else {
        a[i] = -abs(a[i]);
        b[i] = abs(b[i]);
      }
      cnta[{a[i], b[i]}]++;
      if (a[i] < 0) {
        a[i] *= -1;
      } else {
        b[i] *= -1;
      }
      cntb[{b[i], a[i]}]++;
    } else if (a[i] == 0 && b[i] == 0) {
      sum--;
      z++;
    } else if (a[i] == 0) {
      za++;
    } else if (b[i] == 0) {
      zb++;
    }
  }
  vector<pll> cnt;
  set<pll> al;
  for (auto p : cnta) {
    ll f = p.first.first;
    ll s = p.first.second;
    if (f < 0)
      f *= -1;
    else
      s *= -1;
    al.insert(pll{s, f});
    if (al.count(p.first))
      continue;
    if (cntb[p.first])
      cnt.push_back({p.second, cntb[p.first]});
  }
  if (za * zb != 0)
    cnt.push_back({za, zb});
  for (auto p : cnt) {
    sum -= p.first + p.second;
  }
  ans = mpow(2, sum);
  for (auto p : cnt) {
    ans = (ans * (mpow(2, p.first) + mpow(2, p.second) - 1 + mod) % mod);
  }
  ans = (ans + z) % mod;
  cout << (ans - 1 + mod) % mod << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> pll;
const ll mod = 1e9 + 7;
// const ll mod=998244353;
const ll inf = 1LL << 61;

ll mpow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return ans;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  map<pll, ll> cnta, cntb;
  ll sum = n;
  ll ans = 0;
  ll z = 0;
  ll za = 0;
  ll zb = 0;
  for (ll i = 0; i < n; i++) {
    if (a[i] * b[i] != 0) {
      for (ll j = 2; j * j <= min(abs(a[i]), abs(b[i])); j++) {
        while (a[i] % j == 0 && b[i] % j == 0) {
          a[i] /= j;
          b[i] /= j;
        }
      }
      ll tmp = min(abs(a[i]), abs(b[i]));
      if (a[i] % tmp == 0 && b[i] % tmp == 0) {
        a[i] /= tmp;
        b[i] /= tmp;
      }
      if (a[i] * b[i] > 0) {
        a[i] = abs(a[i]);
        b[i] = abs(b[i]);
      } else {
        a[i] = -abs(a[i]);
        b[i] = abs(b[i]);
      }
      cnta[{a[i], b[i]}]++;
      if (a[i] < 0) {
        a[i] *= -1;
      } else {
        b[i] *= -1;
      }
      cntb[{b[i], a[i]}]++;
    } else if (a[i] == 0 && b[i] == 0) {
      sum--;
      z++;
    } else if (a[i] == 0) {
      za++;
    } else if (b[i] == 0) {
      zb++;
    }
  }
  vector<pll> cnt;
  set<pll> al;
  for (auto p : cnta) {
    ll f = p.first.first;
    ll s = p.first.second;
    if (f < 0)
      f *= -1;
    else
      s *= -1;
    al.insert(pll{s, f});
    if (al.count(p.first))
      continue;
    if (cntb[p.first])
      cnt.push_back({p.second, cntb[p.first]});
  }
  if (za * zb != 0)
    cnt.push_back({za, zb});
  for (auto p : cnt) {
    sum -= p.first + p.second;
  }
  ans = mpow(2, sum);
  for (auto p : cnt) {
    ans = (ans * (mpow(2, p.first) + mpow(2, p.second) - 1 + mod) % mod);
  }
  ans = (ans + z) % mod;
  cout << (ans - 1 + mod) % mod << endl;
}