Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

template <int64_t p> struct Fp {
  Fp(int64_t n) : n(n % p) {}
  Fp operator+(const Fp &rhs) { return Fp(n + rhs.n); }
  Fp operator*(const Fp &rhs) { return Fp(n * rhs.n); }
  int64_t n;
};

constexpr int64_t P = 998244353;

struct Robot {
  int64_t x;
  int64_t d;
};

void indent(size_t dep) {
  for (size_t i = 0; i < dep; i++) {
    std::cerr << ' ';
  }
}

std::pair<size_t, Fp<P>> calc_combi(const std::vector<Robot> robots,
                                    size_t begin, int64_t end_x) {
  static size_t dep = 0;
  dep++;
  // indent(dep);
  // std::cerr << begin << " at " << robots[begin].x << ' ' << end_x  <<
  // std::endl;
  if (begin >= robots.size()) {
    throw "out of index";
  }
  if (robots[begin].x >= end_x) {
    throw "out of x bound";
  }

  size_t head = begin;
  Fp<P> accum = 1;
  while (head < robots.size()) {
    const Robot &r = robots[head];
    if (r.x >= end_x) {
      break;
    }
    if (head + 1 >= robots.size()) {
      head = head + 1;
      accum = accum * 2;
      break;
    }
    const Robot &next = robots[head + 1];
    if (next.x >= r.x + r.d) {
      head = head + 1;
      accum = accum * 2;
    } else {
      auto p = calc_combi(robots, head + 1, r.x + r.d);
      head = p.first;
      accum = accum * (p.second + 1);
    }
  }
  // indent(dep);
  // std::cerr << begin << " at " << robots[begin].x << ' '  << end_x <<
  //     " -> " << head << ' ' << accum.n << std::endl;
  dep--;
  return {head, accum};
}

int main() {
  try {
    size_t n;
    scanf("%ld", &n);
    std::vector<Robot> robots;
    for (size_t i = 0; i < n; i++) {
      Robot r;
      scanf("%ld %ld", &r.x, &r.d);
      robots.push_back(r);
    }
    std::sort(robots.begin(), robots.end(),
              [](const Robot &a, const Robot &b) { return a.x < b.x; });
    auto result = calc_combi(robots, 0, robots.back().x + 1);
    std::cout << result.second.n << std::endl;
  } catch (const char *e) {
    // std::cerr << e << std::endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>

template <int64_t p> struct Fp {
  Fp(int64_t n) : n(n % p) {}
  Fp operator+(const Fp &rhs) { return Fp(n + rhs.n); }
  Fp operator*(const Fp &rhs) { return Fp(n * rhs.n); }
  int64_t n;
};

constexpr int64_t P = 998244353;

struct Robot {
  int64_t x;
  int64_t d;
};

void indent(size_t dep) {
  for (size_t i = 0; i < dep; i++) {
    std::cerr << ' ';
  }
}

std::pair<size_t, Fp<P>> calc_combi(const std::vector<Robot> &robots,
                                    size_t begin, int64_t end_x) {
  static size_t dep = 0;
  dep++;
  // indent(dep);
  // std::cerr << begin << " at " << robots[begin].x << ' ' << end_x  <<
  // std::endl;
  if (begin >= robots.size()) {
    throw "out of index";
  }
  if (robots[begin].x >= end_x) {
    throw "out of x bound";
  }

  size_t head = begin;
  Fp<P> accum = 1;
  while (head < robots.size()) {
    const Robot &r = robots[head];
    if (r.x >= end_x) {
      break;
    }
    if (head + 1 >= robots.size()) {
      head = head + 1;
      accum = accum * 2;
      break;
    }
    const Robot &next = robots[head + 1];
    if (next.x >= r.x + r.d) {
      head = head + 1;
      accum = accum * 2;
    } else {
      auto p = calc_combi(robots, head + 1, r.x + r.d);
      head = p.first;
      accum = accum * (p.second + 1);
    }
  }
  // indent(dep);
  // std::cerr << begin << " at " << robots[begin].x << ' '  << end_x <<
  //     " -> " << head << ' ' << accum.n << std::endl;
  dep--;
  return {head, accum};
}

int main() {
  try {
    size_t n;
    scanf("%ld", &n);
    std::vector<Robot> robots;
    for (size_t i = 0; i < n; i++) {
      Robot r;
      scanf("%ld %ld", &r.x, &r.d);
      robots.push_back(r);
    }
    std::sort(robots.begin(), robots.end(),
              [](const Robot &a, const Robot &b) { return a.x < b.x; });
    auto result = calc_combi(robots, 0, robots.back().x + 1);
    std::cout << result.second.n << std::endl;
  } catch (const char *e) {
    // std::cerr << e << std::endl;
  }
  return 0;
}