Problem p02956 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 4e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

pii A[MAXN];
ll bi[MAXN];
char S[MAXN];
int tree[MAXN][3];
int B[MAXN];
int C[MAXN];
vector<int> xpos;
vector<int> ypos;

void update(int tmp, int j) {

  tree[tmp][j]++;
  tmp >>= 1;

  while (tmp) {
    tree[tmp][j] = tree[tmp * 2][j] + tree[tmp * 2 + 1][j];
    tmp >>= 1;
  }
  return;
}

int getans(int L, int R, int j) {
  ll res = 0;

  while (L <= R) {
    if (L & 1) {
      res += tree[L][j];
      L++;
    }
    if (!(R & 1)) {
      res += tree[R][j];
      R--;
    }
    L >>= 1;
    R >>= 1;
  }

  return res;
}

int main() {
  int n, m, k, x, y;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  int idx = -1, idy = -1;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i].f >> A[i].s;
    xpos.push_back(A[i].f);
    ypos.push_back(A[i].s);
  }

  sort(xpos.begin(), xpos.end());
  sort(ypos.begin(), ypos.end());

  sort(A, A + n);

  bi[0] = 1;

  for (int i = 1; i <= n; i++) {
    bi[i] = bi[i - 1] * 2;
    bi[i] %= PMOD;
  }

  int base = 1;

  for (base; base < n; base *= 2)
    ;

  for (int i = 0; i < n; i++) {

    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    C[i] = getans(base, base + idy, 0);
    update(base + idy, 0);
  }

  for (int i = n - 1; i >= 0; i--) {

    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    B[i] = getans(base + idy, base * 2 - 1, 1);
    update(base + idy, 1);
  }
  ll res = 0;
  ll a, b, c, d;
  ll cur = 0;
  for (int i = 0; i < n; i++) {
    idx = lower_bound(xpos.begin(), xpos.end(), A[i].f) - xpos.begin();
    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    a = idx - C[i];
    b = B[i];
    c = C[i];
    d = (n - 1 - idx) - B[i];
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
    cur = bi[a + b] + bi[a + c] + bi[b + d] + bi[c + d];
    cur %= PMOD;
    cur -= (bi[a] + bi[b] + bi[c] + bi[d]) % PMOD;
    cur = (bi[n] - 1) - cur;
    // cout<<cur<<"\n";
    res = (res + cur + PMOD) % PMOD;
  }

  cout << res << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9 + 10;
const ll LINF = 1LL * INF * INF;
const int MAXN = 8e5 + 10;
const int MAXM = 5e3 + 10;

priority_queue<int> pq;
vector<vector<int>> graph;
queue<int> que;

pii A[MAXN];
ll bi[MAXN];
char S[MAXN];
int tree[MAXN][3];
int B[MAXN];
int C[MAXN];
vector<int> xpos;
vector<int> ypos;

void update(int tmp, int j) {

  tree[tmp][j]++;
  tmp >>= 1;

  while (tmp) {
    tree[tmp][j] = tree[tmp * 2][j] + tree[tmp * 2 + 1][j];
    tmp >>= 1;
  }
  return;
}

int getans(int L, int R, int j) {
  ll res = 0;

  while (L <= R) {
    if (L & 1) {
      res += tree[L][j];
      L++;
    }
    if (!(R & 1)) {
      res += tree[R][j];
      R--;
    }
    L >>= 1;
    R >>= 1;
  }

  return res;
}

int main() {
  int n, m, k, x, y;
  int cnt = 0;
  int mx = 0;
  int mn = INF;
  int idx = -1, idy = -1;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> A[i].f >> A[i].s;
    xpos.push_back(A[i].f);
    ypos.push_back(A[i].s);
  }

  sort(xpos.begin(), xpos.end());
  sort(ypos.begin(), ypos.end());

  sort(A, A + n);

  bi[0] = 1;

  for (int i = 1; i <= n; i++) {
    bi[i] = bi[i - 1] * 2;
    bi[i] %= PMOD;
  }

  int base = 1;

  for (base; base < n; base *= 2)
    ;

  for (int i = 0; i < n; i++) {

    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    C[i] = getans(base, base + idy, 0);
    update(base + idy, 0);
  }

  for (int i = n - 1; i >= 0; i--) {

    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    B[i] = getans(base + idy, base * 2 - 1, 1);
    update(base + idy, 1);
  }
  ll res = 0;
  ll a, b, c, d;
  ll cur = 0;
  for (int i = 0; i < n; i++) {
    idx = lower_bound(xpos.begin(), xpos.end(), A[i].f) - xpos.begin();
    idy = lower_bound(ypos.begin(), ypos.end(), A[i].s) - ypos.begin();

    a = idx - C[i];
    b = B[i];
    c = C[i];
    d = (n - 1 - idx) - B[i];
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
    cur = bi[a + b] + bi[a + c] + bi[b + d] + bi[c + d];
    cur %= PMOD;
    cur -= (bi[a] + bi[b] + bi[c] + bi[d]) % PMOD;
    cur = (bi[n] - 1) - cur;
    // cout<<cur<<"\n";
    res = (res + cur + PMOD) % PMOD;
  }

  cout << res << "\n";

  return 0;
}