Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MOD = 998244353;

int mul(int a, int b) { return a * 1ll * b % MOD; }

int add(int a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
  return a;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
#endif
  int n;
  cin >> n;

  vector<int> r(n), c(n), power(n + 1);
  vector<array<int, 2>> p(n);
  vector<array<int, 4>> ans(n);

  power[0] = 1;
  for (int i = 0; i < n; i++) {
    cin >> r[i] >> c[i];
    p[i] = {r[i], c[i]};
    power[i + 1] = mul(power[i], 2);
  }

  vector<int> ind(n);
  iota(ind.begin(), ind.end(), 0);

  sort(ind.begin(), ind.end(), [&](int i, int j) { return p[i][0] < p[j][0]; });

  sort(r.begin(), r.end());
  r.resize(unique(r.begin(), r.end()) - r.begin());

  Tree<int> os;
  int j = 0;
  for (int i = 0; i < r.size(); i++) {
    int st = j;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      os.insert(p[ind[j]][1]);
      j++;
    }

    j = st;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      int lf = os.order_of_key(p[ind[j]][1] + 1);
      int rt = os.size() - os.order_of_key(p[ind[j]][1]);
      ans[ind[j]][2] = lf - 1;
      ans[ind[j]][0] = rt - 1;
      j++;
    }
  }

  sort(ind.begin(), ind.end(), [&](int i, int j) { return p[i][0] > p[j][0]; });

  reverse(r.begin(), r.end());
  os.clear();

  j = 0;
  for (int i = 0; i < r.size(); i++) {
    int st = j;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      os.insert(p[ind[j]][1]);
      j++;
    }

    j = st;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      int lf = os.order_of_key(p[ind[j]][1] + 1);
      int rt = os.size() - os.order_of_key(p[ind[j]][1]);
      ans[ind[j]][3] = lf - 1;
      ans[ind[j]][1] = rt - 1;
      j++;
    }
  }

  int res = mul(n, power[n - 1]);
  for (int i = 0; i < n; i++) {
    int a = ans[i][0], b = ans[i][1];
    int c = ans[i][2], d = ans[i][3];
    res = add(res, mul(mul(power[a] - 1, power[d] - 1), power[c]));
    res = add(res, mul(mul(power[a] - 1, power[d] - 1), power[b] - 1));
    res = add(res, mul(mul(power[b] - 1, power[c] - 1), power[a]));
    res = add(res, mul(mul(power[b] - 1, power[c] - 1), power[d] - 1));
    res = add(res, mul(power[a] - 1,
                       mul(power[b] - 1, mul(power[c] - 1, power[d] - 1))));
  }

  cout << res;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MOD = 998244353;

int mul(int a, int b) { return a * 1ll * b % MOD; }

int add(int a, int b) {
  a += b;
  while (a >= MOD)
    a -= MOD;
  while (a < 0)
    a += MOD;
  return a;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;

  vector<int> r(n), c(n), power(n + 1);
  vector<array<int, 2>> p(n);
  vector<array<int, 4>> ans(n);

  power[0] = 1;
  for (int i = 0; i < n; i++) {
    cin >> r[i] >> c[i];
    p[i] = {r[i], c[i]};
    power[i + 1] = mul(power[i], 2);
  }

  vector<int> ind(n);
  iota(ind.begin(), ind.end(), 0);

  sort(ind.begin(), ind.end(), [&](int i, int j) { return p[i][0] < p[j][0]; });

  sort(r.begin(), r.end());
  r.resize(unique(r.begin(), r.end()) - r.begin());

  Tree<int> os;
  int j = 0;
  for (int i = 0; i < r.size(); i++) {
    int st = j;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      os.insert(p[ind[j]][1]);
      j++;
    }

    j = st;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      int lf = os.order_of_key(p[ind[j]][1] + 1);
      int rt = os.size() - os.order_of_key(p[ind[j]][1]);
      ans[ind[j]][2] = lf - 1;
      ans[ind[j]][0] = rt - 1;
      j++;
    }
  }

  sort(ind.begin(), ind.end(), [&](int i, int j) { return p[i][0] > p[j][0]; });

  reverse(r.begin(), r.end());
  os.clear();

  j = 0;
  for (int i = 0; i < r.size(); i++) {
    int st = j;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      os.insert(p[ind[j]][1]);
      j++;
    }

    j = st;
    while (j < ind.size() && p[ind[j]][0] == r[i]) {
      int lf = os.order_of_key(p[ind[j]][1] + 1);
      int rt = os.size() - os.order_of_key(p[ind[j]][1]);
      ans[ind[j]][3] = lf - 1;
      ans[ind[j]][1] = rt - 1;
      j++;
    }
  }

  int res = mul(n, power[n - 1]);
  for (int i = 0; i < n; i++) {
    int a = ans[i][0], b = ans[i][1];
    int c = ans[i][2], d = ans[i][3];
    res = add(res, mul(mul(power[a] - 1, power[d] - 1), power[c]));
    res = add(res, mul(mul(power[a] - 1, power[d] - 1), power[b] - 1));
    res = add(res, mul(mul(power[b] - 1, power[c] - 1), power[a]));
    res = add(res, mul(mul(power[b] - 1, power[c] - 1), power[d] - 1));
    res = add(res, mul(power[a] - 1,
                       mul(power[b] - 1, mul(power[c] - 1, power[d] - 1))));
  }

  cout << res;
}