Problem p02956 (C++)

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

/* FastIO by rsk0315 (last update: 2018/02/21 00:36) */

namespace FastIn {
static constexpr size_t BUF_SIZE = 1 << 17, INT_LEN = 24, FLT_LEN = 400;
static char buf[BUF_SIZE | 1] = {}, *pos = buf, *endbuf = nullptr;
FILE *fin;

inline bool rebuffer() {
  // returns true <=> there is at least one unread character
  size_t rest = endbuf - pos;
  if (buf + rest > pos) {
    // buf[:pos] and buf[-pos:] are overlapping, which std::memcpy()
    // causes undefined behavior.
    return true;
  }

  std::memcpy(buf, pos, rest);
  pos = buf;
  size_t len = std::fread(pos + rest, 1, BUF_SIZE - rest, fin);
  *(endbuf = buf + (rest + len)) = 0;

  return *pos;
}

inline bool scan(char &in) {
  if ((in = *pos)) {
    ++pos;
    return true;
  }

  return rebuffer() && (in = *pos++);
}

inline bool scan(char *in) {
  if ((*in = *pos) == 0) {
    if (rebuffer() && (*in = *pos) == 0) {
      return false;
    }
  }
  ++in;
  while (true) {
    if ((*in = *pos++) == 0) {
      if (rebuffer() && (*in = *pos++) == 0) {
        return true;
      }
    }
    ++in;
  }
}

inline bool scan(double &in) {
  if (pos + FLT_LEN >= endbuf && !rebuffer()) {
    in = 0.0;
    return false;
  }

  char *tmp;
  in = std::strtod(pos, &tmp);
  pos = tmp;
  return true;
}

template <class Int> inline bool scan(Int &in) {
  in = 0;

  // assume that no redundant whitespace appears
  if (pos + INT_LEN >= endbuf && !rebuffer()) {
    return false;
  }

  if (std::is_signed<Int>::value) {
    if (*pos == '-') {
      in = ~*++pos + '1';
      while (*++pos >= '0') {
        in = in * 10 + ~*pos + '1';
      }
      ++pos;
      return true;
    }
  }

  // assume that numbers are separated by the character whose value is
  // less than '0' (e.g. whitespaces, newlines)
  do {
    in = in * 10 + *pos - '0';
  } while (*++pos >= '0');
  ++pos;
  return true;
}

inline bool eat() {
  if (*pos > ' ') {
    return true;
  }

  do {
    if (*pos == 0 && !rebuffer()) {
      return false;
    }
  } while (*++pos <= ' ');

  return true;
}

inline bool eat(char ch) {
  if (*pos == ch) {
    return true;
  }

  do {
    if (*pos == 0 && !rebuffer()) {
      return false;
    }
  } while (*++pos != ch);

  return true;
}

class Scanner {
  bool rebuffer() { return FastIn::rebuffer(); }

public:
  Scanner(FILE *fin = stdin) {
    FastIn::fin = fin;
    endbuf = pos + std::fread(buf, 1, BUF_SIZE, fin);
  }

  template <class T> inline bool scan(T &in) { return FastIn::scan(in); }

  template <class First, class... Rest>
  inline bool scan(First &in, Rest &...ins) {
    return scan(in) && scan(ins...);
  }
};
} // namespace FastIn

template <class RandomIt>
auto rsort(RandomIt first, RandomIt last) -> typename std::enable_if<
    std::is_integral<typename RandomIt::value_type::first_type>::value>::type {
  using PairType = typename RandomIt::value_type;
  using KeyType = typename PairType::first_type;

  intmax_t cur_shift = 0, max_exp = sizeof(KeyType);
  std::vector<PairType> work(last - first);
  for (int i = 0; i < max_exp; ++i) {
    size_t num[256] = {};

    for (RandomIt it = first; it < last; ++it)
      ++num[((it->first) >> cur_shift) & 255];

    {
      size_t tmp = 0;
      for (int j = 0; j < 256; ++j)
        std::swap(tmp, num[j] += tmp);
    }

    for (RandomIt it = first; it < last; ++it)
      work[num[((it->first) >> cur_shift) & 255]++] = *it;

    {
      size_t j = 0;
      for (RandomIt it = first; it < last; ++it)
        *it = work[j++];
    }

    cur_shift += 8;
  }

  if (!std::is_signed<KeyType>::value)
    return;

  RandomIt mid = last;
  for (RandomIt it = last; --it >= first;)
    if ((it->first) >= 0) {
      mid = ++it;
      break;
    }

  if (mid == last)
    return;

  std::copy(first, mid, work.begin() + (last - mid));
  std::copy(mid, last, work.begin());
  std::copy(work.begin(), work.end(), first);
}

/* kokomade */

int main() {
  using namespace std;
  FastIn::Scanner in;
  unsigned long N;
  in.scan(N);
  vector<unsigned long> perm(N);
  {
    vector<pair<long, long>> p(N);
    for (auto &i : p)
      in.scan(i.first, i.second);
    rsort(p.begin(), p.end());
    for (unsigned long i = 0; i < N; ++i)
      p[i] = {p[i].second, i + 1};
    rsort(p.begin(), p.end());
    transform(p.cbegin(), p.cend(), perm.begin(),
              [](auto i) { return i.second; });
  }

  constexpr unsigned long MOD = 998244353;
  vector<unsigned long> binary(N + 1);
  unsigned long n = binary[0] = 1;
  generate(binary.begin() + 1, binary.end(), [&n]() {
    if (n >= MOD)
      n -= MOD;
    return n <<= 1;
  });

  unsigned long N_{*lower_bound(binary.begin(), binary.begin() + 29, N)};

  vector<unsigned long> fenwick_tree(N_ + 1);
  auto que = [&fenwick_tree, &N_](unsigned long i) {
    unsigned long s{0}, j{i};
    while (i) {
      s += fenwick_tree[i];
      i -= i & -i;
    }
    while (j <= N_) {
      ++fenwick_tree[j];
      j += j & -j;
    }
    return s;
  };

  vector<unsigned long> coef(N + 1);
  coef[N] = N + MOD - 4;
  for (unsigned long i = 0; i != N; ++i) {
    auto q = que(perm[i]);
    ++coef[q];
    ++coef[i - q];
    ++coef[perm[i] - q - 1];
    ++coef[N - i - perm[i] + q];
  }

  cout << inner_product(binary.begin(), binary.end(), coef.begin(),
                        4 + MOD - N) %
              MOD
       << endl;
  return 0;
}

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

/* FastIO by rsk0315 (last update: 2018/02/21 00:36) */

namespace FastIn {
static constexpr size_t BUF_SIZE = 1 << 17, INT_LEN = 24, FLT_LEN = 400;
static char buf[BUF_SIZE | 1] = {}, *pos = buf, *endbuf = nullptr;
FILE *fin;

inline bool rebuffer() {
  // returns true <=> there is at least one unread character
  size_t rest = endbuf - pos;
  if (buf + rest > pos) {
    // buf[:pos] and buf[-pos:] are overlapping, which std::memcpy()
    // causes undefined behavior.
    return true;
  }

  std::memcpy(buf, pos, rest);
  pos = buf;
  size_t len = std::fread(pos + rest, 1, BUF_SIZE - rest, fin);
  *(endbuf = buf + (rest + len)) = 0;

  return *pos;
}

inline bool scan(char &in) {
  if ((in = *pos)) {
    ++pos;
    return true;
  }

  return rebuffer() && (in = *pos++);
}

inline bool scan(char *in) {
  if ((*in = *pos) == 0) {
    if (rebuffer() && (*in = *pos) == 0) {
      return false;
    }
  }
  ++in;
  while (true) {
    if ((*in = *pos++) == 0) {
      if (rebuffer() && (*in = *pos++) == 0) {
        return true;
      }
    }
    ++in;
  }
}

inline bool scan(double &in) {
  if (pos + FLT_LEN >= endbuf && !rebuffer()) {
    in = 0.0;
    return false;
  }

  char *tmp;
  in = std::strtod(pos, &tmp);
  pos = tmp;
  return true;
}

template <class Int> inline bool scan(Int &in) {
  in = 0;

  // assume that no redundant whitespace appears
  if (pos + INT_LEN >= endbuf && !rebuffer()) {
    return false;
  }

  if (std::is_signed<Int>::value) {
    if (*pos == '-') {
      in = ~*++pos + '1';
      while (*++pos >= '0') {
        in = in * 10 + ~*pos + '1';
      }
      ++pos;
      return true;
    }
  }

  // assume that numbers are separated by the character whose value is
  // less than '0' (e.g. whitespaces, newlines)
  do {
    in = in * 10 + *pos - '0';
  } while (*++pos >= '0');
  ++pos;
  return true;
}

inline bool eat() {
  if (*pos > ' ') {
    return true;
  }

  do {
    if (*pos == 0 && !rebuffer()) {
      return false;
    }
  } while (*++pos <= ' ');

  return true;
}

inline bool eat(char ch) {
  if (*pos == ch) {
    return true;
  }

  do {
    if (*pos == 0 && !rebuffer()) {
      return false;
    }
  } while (*++pos != ch);

  return true;
}

class Scanner {
  bool rebuffer() { return FastIn::rebuffer(); }

public:
  Scanner(FILE *fin = stdin) {
    FastIn::fin = fin;
    endbuf = pos + std::fread(buf, 1, BUF_SIZE, fin);
  }

  template <class T> inline bool scan(T &in) { return FastIn::scan(in); }

  template <class First, class... Rest>
  inline bool scan(First &in, Rest &...ins) {
    return scan(in) && scan(ins...);
  }
};
} // namespace FastIn

template <class RandomIt>
auto rsort(RandomIt first, RandomIt last) -> typename std::enable_if<
    std::is_integral<typename RandomIt::value_type::first_type>::value>::type {
  using PairType = typename RandomIt::value_type;
  using KeyType = typename PairType::first_type;

  intmax_t cur_shift = 0, max_exp = sizeof(KeyType);
  std::vector<PairType> work(last - first);
  for (int i = 0; i < max_exp; ++i) {
    size_t num[256] = {};

    for (RandomIt it = first; it < last; ++it)
      ++num[((it->first) >> cur_shift) & 255];

    {
      size_t tmp = 0;
      for (int j = 0; j < 256; ++j)
        std::swap(tmp, num[j] += tmp);
    }

    for (RandomIt it = first; it < last; ++it)
      work[num[((it->first) >> cur_shift) & 255]++] = *it;

    {
      size_t j = 0;
      for (RandomIt it = first; it < last; ++it)
        *it = work[j++];
    }

    cur_shift += 8;
  }

  if (!std::is_signed<KeyType>::value)
    return;

  RandomIt mid = last;
  for (RandomIt it = last; --it >= first;)
    if ((it->first) >= 0) {
      mid = ++it;
      break;
    }

  if (mid == last)
    return;

  std::copy(first, mid, work.begin() + (last - mid));
  std::copy(mid, last, work.begin());
  std::copy(work.begin(), work.end(), first);
}

/* kokomade */

int main() {
  using namespace std;
  FastIn::Scanner in;
  unsigned long N;
  in.scan(N);
  vector<unsigned long> perm(N);
  {
    vector<pair<long, long>> p(N);
    for (auto &i : p)
      in.scan(i.first, i.second);
    rsort(p.begin(), p.end());
    for (unsigned long i = 0; i < N; ++i)
      p[i] = {p[i].second, i + 1};
    rsort(p.begin(), p.end());
    transform(p.cbegin(), p.cend(), perm.begin(),
              [](auto i) { return i.second; });
  }

  constexpr unsigned long MOD = 998244353;
  vector<unsigned long> binary(N + 1);
  unsigned long n = binary[0] = 1;
  generate(binary.begin() + 1, binary.end(), [&n]() {
    if (n >= MOD)
      n -= MOD;
    return n <<= 1;
  });

  unsigned long N_{2};
  {
    while (N_ <= N)
      N_ <<= 1;
  }
  vector<unsigned long> fenwick_tree(N_ + 1);
  auto que = [&fenwick_tree, &N_](unsigned long i) {
    unsigned long s{0}, j{i};
    while (i) {
      s += fenwick_tree[i];
      i -= i & -i;
    }
    while (j <= N_) {
      ++fenwick_tree[j];
      j += j & -j;
    }
    return s;
  };

  vector<unsigned long> coef(N + 1);
  coef[N] = N + MOD - 4;
  for (unsigned long i = 0; i != N; ++i) {
    auto q = que(perm[i]);
    ++coef[q];
    ++coef[i - q];
    ++coef[perm[i] - q - 1];
    ++coef[N - i - perm[i] + q];
  }

  cout << inner_product(binary.begin(), binary.end(), coef.begin(),
                        4 + MOD - N) %
              MOD
       << endl;
  return 0;
}