Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 998244353

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

ll fpow(ll x, ll p) {
  if (p == 0)
    return 1;
  return ((p & 1) ? x : 1) * fpow(x * x % MOD, p >> 1) % MOD;
}

ll subs(ll x, ll d) { return ((x - d) % MOD + MOD) % MOD; }

ll comb(ll n) {
  if (n & 1)
    return (n - 1) / 2 * n % MOD;
  return n / 2 * (n - 1) % MOD;
}

class Fnwick {
  int *ar;
  int size;

public:
  Fnwick(int n) {
    size = n + 1;
    ar = new int[size];
    memset(ar, 0, sizeof(int) * size);
  }
  ~Fnwick() { delete ar; }
  int add(int x, int v) {
    for (int ctr1 = x + 1; ctr1 <= size; ctr1 += (ctr1 & -ctr1))
      ar[ctr1] += v;
  }
  int get(int x) {
    int rez = 0;
    for (int ctr1 = x + 1; ctr1 > 0; ctr1 -= (ctr1 & -ctr1))
      rez += ar[ctr1];
    return rez;
  }
};

ll count(vector<ii> &p, unordered_map<int, int> &rc, int n) {
  ll rez = 0;
  Fnwick bit(n);
  for (int ctr1 = 0; ctr1 < n; ++ctr1) {
    rez = subs(rez, fpow(2, ctr1));
    rez = subs(rez, fpow(2, n - 1 - ctr1));
    int y = rc.find(p[ctr1].second)->second;
    int lower = bit.get(y);
    int higher = ctr1 - lower;
    rez = (rez + fpow(2, lower)) % MOD;
    rez = (rez + fpow(2, higher)) % MOD;
    bit.add(y, 1);
  }
  return rez;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;

  int x, y;
  vector<ii> points;
  vector<int> compressed;
  unordered_map<int, int> rev;
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    cin >> x >> y, points.push_back(make_pair(x, y)), compressed.push_back(y);

  sort(points.begin(), points.end());
  sort(compressed.begin(), compressed.end());
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    rev.emplace(compressed[ctr1], ctr1);

  ll rez = subs(fpow(2, n), 1) * n % MOD;
  rez = (rez + count(points, rev, n)) % MOD;
  reverse(points.begin(), points.end());
  rez = (rez + count(points, rev, n)) % MOD;

  cout << rez << "\n";

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 998244353

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

ll fpow(ll x, ll p) {
  if (p == 0)
    return 1;
  return ((p & 1) ? x : 1) * fpow(x * x % MOD, p >> 1) % MOD;
}

ll subs(ll x, ll d) { return ((x - d) % MOD + MOD) % MOD; }

ll comb(ll n) {
  if (n & 1)
    return (n - 1) / 2 * n % MOD;
  return n / 2 * (n - 1) % MOD;
}

class Fnwick {
  int *ar;
  int size;

public:
  Fnwick(int n) {
    size = n + 1;
    ar = new int[size];
    memset(ar, 0, sizeof(int) * size);
  }
  ~Fnwick() { delete ar; }
  int add(int x, int v) {
    for (int ctr1 = x + 1; ctr1 <= size; ctr1 += (ctr1 & -ctr1))
      ar[ctr1] += v;
  }
  int get(int x) {
    int rez = 0;
    for (int ctr1 = x + 1; ctr1 > 0; ctr1 -= (ctr1 & -ctr1))
      rez += ar[ctr1];
    return rez;
  }
};

ll count(vector<ii> &p, unordered_map<int, int> &rc, int n) {
  ll rez = 0;
  Fnwick bit(2 * n);
  for (int ctr1 = 0; ctr1 < n; ++ctr1) {
    rez = subs(rez, fpow(2, ctr1));
    rez = subs(rez, fpow(2, n - 1 - ctr1));
    int y = rc.find(p[ctr1].second)->second;
    int lower = bit.get(y);
    int higher = ctr1 - lower;
    rez = (rez + fpow(2, lower)) % MOD;
    rez = (rez + fpow(2, higher)) % MOD;
    bit.add(y, 1);
  }
  return rez;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;

  int x, y;
  vector<ii> points;
  vector<int> compressed;
  unordered_map<int, int> rev;
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    cin >> x >> y, points.push_back(make_pair(x, y)), compressed.push_back(y);

  sort(points.begin(), points.end());
  sort(compressed.begin(), compressed.end());
  for (int ctr1 = 0; ctr1 < n; ++ctr1)
    rev.emplace(compressed[ctr1], ctr1);

  ll rez = subs(fpow(2, n), 1) * n % MOD;
  rez = (rez + count(points, rev, n)) % MOD;
  reverse(points.begin(), points.end());
  rez = (rez + count(points, rev, n)) % MOD;

  cout << rez << "\n";

  return 0;
}