Problem p02956 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 998244353LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

struct BIT {
  ll bit[1000010];
  void add(ll a, ll w) {
    for (ll x = a; x < 1000010; x += (x & -x))
      bit[x] += w;
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= (x & -x))
      ret += bit[x];
    return ret;
  }
};

ll pow(ll a, ll n, ll m) {
  ll ret = 1LL;
  for (; n > 0LL; n >>= 1LL, a = a * a % m) {
    if (n % 2LL == 1LL) {
      ret = ret * a % m;
    }
  }
  return ret;
}

int main() {
  ll N;
  cin >> N;
  vector<P> v(N);
  rep(i, N) cin >> v[i].first >> v[i].second;
  sort(v.begin(), v.end());
  vector<ll> y(N), Y(N);
  rep(i, N) y[i] = Y[i] = v[i].second;
  sort(y.begin(), y.end());
  rep(i, N) {
    ll s = find(y.begin(), y.end(), Y[i]) - y.begin() + 1;
    Y[i] = s;
  }
  BIT B;
  vector<vector<ll>> cnt(N, vector<ll>(4));
  rep(i, N) {
    cnt[i][1] = B.sum(Y[i]);
    B.add(Y[i], 1);
  }
  BIT C;
  rep(i, N) {
    cnt[N - i - 1][2] = C.sum(Y[N - i - 1]);
    C.add(Y[N - i - 1], 1);
  }
  rep(i, N) {
    cnt[i][0] = i - cnt[i][1];
    cnt[i][3] = N - i - 1 - cnt[i][2];
  }
  ll al = pow(2LL, N, mod) - 1;
  ll ans = 0;
  rep(i, N) {
    ll t = 0;
    rep(j, 4) {
      t += (pow(2LL, cnt[i][j] + cnt[i][(j + 1) % 4], mod) -
            pow(2LL, cnt[i][j], mod)) %
           mod;
    }
    ans = (ans + (al - t)) % mod;
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 998244353LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

struct BIT {
  ll bit[1000010];
  void add(ll a, ll w) {
    for (ll x = a; x < 1000010; x += (x & -x))
      bit[x] += w;
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= (x & -x))
      ret += bit[x];
    return ret;
  }
};

ll pow(ll a, ll n, ll m) {
  ll ret = 1LL;
  for (; n > 0LL; n >>= 1LL, a = a * a % m) {
    if (n % 2LL == 1LL) {
      ret = ret * a % m;
    }
  }
  return ret;
}

int main() {
  ll N;
  cin >> N;
  vector<P> v(N);
  rep(i, N) cin >> v[i].first >> v[i].second;
  sort(v.begin(), v.end());
  vector<ll> y(N), Y(N);
  rep(i, N) y[i] = Y[i] = v[i].second;
  sort(y.begin(), y.end());
  rep(i, N) {
    ll s = lower_bound(y.begin(), y.end(), Y[i]) - y.begin() + 1;
    Y[i] = s;
  }
  BIT B;
  vector<vector<ll>> cnt(N, vector<ll>(4));
  rep(i, N) {
    cnt[i][1] = B.sum(Y[i]);
    B.add(Y[i], 1);
  }
  BIT C;
  rep(i, N) {
    cnt[N - i - 1][2] = C.sum(Y[N - i - 1]);
    C.add(Y[N - i - 1], 1);
  }
  rep(i, N) {
    cnt[i][0] = i - cnt[i][1];
    cnt[i][3] = N - i - 1 - cnt[i][2];
  }
  ll al = pow(2LL, N, mod) - 1;
  ll ans = 0;
  rep(i, N) {
    ll t = 0;
    rep(j, 4) {
      t += (pow(2LL, cnt[i][j] + cnt[i][(j + 1) % 4], mod) -
            pow(2LL, cnt[i][j], mod)) %
           mod;
    }
    ans = (ans + (al - t)) % mod;
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
}