Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

template <unsigned mod = 998244353> struct mint {
  unsigned val;
  static unsigned get_mod() { return mod; }
  unsigned inv() const {
    int tmp, a = val, b = mod, x = 1, y = 0;
    while (b)
      tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
    if (x < 0)
      x += mod;
    return x;
  }
  mint() : val(0) {}
  mint(ll x) : val(x >= 0 ? x % mod : mod + (x % mod)) {}
  mint pow(ll t) {
    mint res = 1, b = *this;
    while (t) {
      if (t & 1)
        res *= b;
      b *= b;
      t >>= 1;
    }
    return res;
  }
  mint &operator+=(const mint &x) {
    if ((val += x.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator-=(const mint &x) {
    if ((val += mod - x.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator*=(const mint &x) {
    val = ll(val) * x.val % mod;
    return *this;
  }
  mint &operator/=(const mint &x) {
    val = ll(val) * x.inv() % mod;
    return *this;
  }
  mint operator+(const mint &x) const { return mint(*this) += x; }
  mint operator-(const mint &x) const { return mint(*this) -= x; }
  mint operator*(const mint &x) const { return mint(*this) *= x; }
  mint operator/(const mint &x) const { return mint(*this) /= x; }
  bool operator==(const mint &x) const { return val == x.val; }
  bool operator!=(const mint &x) const { return val != x.val; }
};
using Mint = mint<>;
template <typename T> struct factorial {
  vector<T> Fact, Finv, Inv;
  factorial(int maxx) {
    Fact.resize(maxx);
    Finv.resize(maxx);
    Inv.resize(maxx);
    Fact[0] = Fact[1] = Finv[0] = Finv[1] = Inv[1] = 1;
    unsigned mod = Mint::get_mod();
    rep(i, 2, maxx) {
      Fact[i] = Fact[i - 1] * i;
      Inv[i] = Inv[mod % i] * (mod - mod / i);
      Finv[i] = Finv[i - 1] * Inv[i];
    }
  }
  T fact(int n, bool inv = 0) {
    if (inv)
      return Finv[n];
    else
      return Fact[n];
  }
  T inv(int n) { return Inv[n]; }
  T nPr(int n, int r) {
    if (n < 0 || n < r || r < 0)
      return Mint(0);
    else
      return Fact[n] * Finv[n - r];
  }
  T nCr(int n, int r) {
    if (n < 0 || n < r || r < 0)
      return Mint(0);
    else
      return Fact[n] * Finv[r] * Finv[n - r];
  }
};

template <typename T, unsigned p> struct NTT {
  vector<T> rt, irt;
  NTT(int lg = 21) {
    unsigned mod = T::get_mod();
    T prt = p;
    rt.resize(1 << lg, 1);
    irt.resize(1 << lg, 1);
    rep(w, 0, lg) {
      int mask = (1 << w) - 1, t = (mod - 1) >> w;
      T g = prt.pow(t), ig = prt.pow(mod - 1 - t);
      rep(i, 0, mask) {
        rt[mask + i + 1] = g * rt[mask + i];
        irt[mask + i + 1] = ig * irt[mask + i];
      }
    }
  }
  void ntt(vector<T> &f, bool inv = 0) {
    int n = f.size();
    if (inv) {
      for (int i = 1; i < n; i <<= 1)
        for (int j = 0; j < n; j += i * 2)
          rep(k, 0, i) {
            f[i + j + k] *= irt[i * 2 - 1 + k];
            const T tmp = f[j + k] - f[i + j + k];
            f[j + k] += f[i + j + k];
            f[i + j + k] = tmp;
          }
      T mul = T(n).inv();
      rep(i, 0, n) f[i] *= mul;
    } else {
      for (int i = n >> 1; i; i >>= 1)
        for (int j = 0; j < n; j += i * 2)
          rep(k, 0, i) {
            const T tmp = f[j + k] - f[i + j + k];
            f[j + k] += f[i + j + k];
            f[i + j + k] = tmp * rt[i * 2 - 1 + k];
          }
    }
  }
  vector<T> conv(vector<T> a, vector<T> b) {
    int n = a.size() + b.size() - 1,
        m = (n & (n - 1) ? 1 << (32 - __builtin_clz(n)) : n);
    a.resize(m);
    b.resize(m);
    ntt(a);
    ntt(b);
    rep(i, 0, m) a[i] *= b[i];
    ntt(a, 1);
    a.resize(n);
    return a;
  }
};
NTT<Mint, 3> ntt;
vector<Mint> multiply(vector<Mint> a, vector<Mint> b) {
  return ntt.conv(a, b);
};

template <typename T> struct FPS {
  using V = vector<T>;
  V f;
  FPS() {}
  FPS(int _n) : f(_n) {}
  FPS(V _f) { f = _f; }
  T &operator[](const int i) { return f[i]; }
  T eval(Mint x) {
    T res, w = 1;
    for (T v : f)
      res += w * v, w *= x;
    return res;
  }
  int size() const { return f.size(); }
  FPS resize(int n) {
    f.resize(n);
    return *this;
  }
  void shrink() {
    while (!f.empty() and f.back() == 0)
      f.pop_back();
  }
  FPS inv() const {
    assert(f[0] != 0);
    int n = f.size();
    FPS res(n);
    res[0] = f[0].inv();
    for (int k = 1; k < n; k <<= 1) {
      FPS g(k), h(k * 2);
      rep(i, 0, min(k, n)) g[i] = res[i];
      rep(i, 0, min(k * 2, n)) h[i] = f[i];
      g *= g;
      g *= h;
      g.resize(k * 2);
      rep(i, 0, min(k * 2, n)) res[i] = res[i] * 2 - g[i];
    }
    return res;
  }
  FPS operator+(const FPS &g) const { return FPS(*this) += g; }
  FPS operator-(const FPS &g) const { return FPS(*this) -= g; }
  FPS operator*(const FPS &g) const { return FPS(*this) *= g; }
  FPS operator/(const FPS &g) const { return FPS(*this) /= g; }
  FPS operator%(const FPS &g) const { return FPS(*this) %= g; }
  FPS &operator+=(FPS g) {
    if (g.size() > f.size())
      f.resize(g.size());
    rep(i, 0, g.size()) f[i] += g[i];
    shrink();
    return *this;
  }
  FPS &operator-=(FPS g) {
    if (g.size() > f.size())
      f.resize(g.size());
    rep(i, 0, g.size()) f[i] -= g[i];
    shrink();
    return *this;
  }
  FPS &operator*=(FPS g) {
    f = multiply(f, g.f);
    shrink();
    return *this;
  }
  FPS &operator/=(FPS g) {
    if (g.size() > f.size())
      return *this = FPS();
    reverse(ALL(f));
    reverse(ALL(g.f));
    int n = f.size() - g.size() + 1;
    f.resize(n);
    g.resize(n);
    *this *= g.inv();
    f.resize(n);
    reverse(ALL(f));
    shrink();
    return *this;
  }
  FPS &operator%=(FPS g) {
    *this -= *this / g * g;
    shrink();
    return *this;
  }
  FPS diff() {
    FPS res(f.size() - 1);
    rep(i, 0, res.size()) res[i] = f[i + 1] * (i + 1);
    return res;
  }
  FPS inte() {
    FPS res(f.size() + 1);
    for (int i = res.size() - 1; i; i--)
      res[i] = f[i - 1] / i;
    return res;
  }
  FPS log() {
    assert(f[0] == 1);
    int n = f.size();
    FPS res = diff() * inv();
    res = res.inte();
    return res.resize(n);
  }
  FPS exp() {
    assert(f[0] == 0);
    int n = f.size();
    FPS f(1), g(1);
    f[0] = g[0] = 1;
    for (int k = 1; k < n; k <<= 1) {
      g = (g + g - f * g * g).resize(k);
      FPS q = diff().resize(k - 1);
      FPS w = (q + g * (f.diff() - f * q)).resize(2 * k - 1);
      f = (f + f * (*this - w.inte()).resize(2 * k)).resize(2 * k);
    }
    return f.resize(n);
  }
  FPS shift(int c) {
    int n = f.size();
    FPS res = *this, mul(n);
    factorial<T> fact(n + 1);
    mul[1] = c;
    mul = mul.exp();
    rep(i, 0, n) res[i] *= fact.fact(i);
    reverse(ALL(res.f));
    res *= mul;
    res.resize(n);
    reverse(ALL(res.f));
    rep(i, 0, n) res[i] *= fact.fact(i, 1);
    return res;
  }
  FPS pow(ll t) {
    int n = f.size(), k = 0;
    while (k < n and f[k] == 0)
      k++;
    FPS res(n);
    if (t * k >= n)
      return res;
    n -= t * k;
    FPS tmp(n);
    Mint c = f[k], ic = c.inv();
    rep(i, 0, n) tmp[i] = f[i + k] * ic;
    tmp = tmp.log();
    for (auto &x : tmp.f)
      x *= t;
    tmp = tmp.exp();
    c = c.pow(t);
    rep(i, 0, n) res[i + t * k] = tmp[i] * c;
    return res;
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  FPS<Mint> f(n + 10);
  for (int i = 0; i < n + 10; ++i)
    f[i].val = 0;
  for (int i = 0; i < m; ++i) {
    int L, R;
    cin >> L >> R;
    for (int j = L; j <= R; ++j) {
      f[j].val = 1;
    }
  }
  auto g = f.pow(n);
  f[0].val = 998244353 - 1;
  g[0].val = 998244353 - 1;
  f = f.inv();
  g = g * f;
  if (g.size() >= n) {
    cout << g[n - 1].val << endl;
  } else {
    cout << 0 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

template <unsigned mod = 998244353> struct mint {
  unsigned val;
  static unsigned get_mod() { return mod; }
  unsigned inv() const {
    int tmp, a = val, b = mod, x = 1, y = 0;
    while (b)
      tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
    if (x < 0)
      x += mod;
    return x;
  }
  mint() : val(0) {}
  mint(ll x) : val(x >= 0 ? x % mod : mod + (x % mod)) {}
  mint pow(ll t) {
    mint res = 1, b = *this;
    while (t) {
      if (t & 1)
        res *= b;
      b *= b;
      t >>= 1;
    }
    return res;
  }
  mint &operator+=(const mint &x) {
    if ((val += x.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator-=(const mint &x) {
    if ((val += mod - x.val) >= mod)
      val -= mod;
    return *this;
  }
  mint &operator*=(const mint &x) {
    val = ll(val) * x.val % mod;
    return *this;
  }
  mint &operator/=(const mint &x) {
    val = ll(val) * x.inv() % mod;
    return *this;
  }
  mint operator+(const mint &x) const { return mint(*this) += x; }
  mint operator-(const mint &x) const { return mint(*this) -= x; }
  mint operator*(const mint &x) const { return mint(*this) *= x; }
  mint operator/(const mint &x) const { return mint(*this) /= x; }
  bool operator==(const mint &x) const { return val == x.val; }
  bool operator!=(const mint &x) const { return val != x.val; }
};
using Mint = mint<>;
template <typename T> struct factorial {
  vector<T> Fact, Finv, Inv;
  factorial(int maxx) {
    Fact.resize(maxx);
    Finv.resize(maxx);
    Inv.resize(maxx);
    Fact[0] = Fact[1] = Finv[0] = Finv[1] = Inv[1] = 1;
    unsigned mod = Mint::get_mod();
    rep(i, 2, maxx) {
      Fact[i] = Fact[i - 1] * i;
      Inv[i] = Inv[mod % i] * (mod - mod / i);
      Finv[i] = Finv[i - 1] * Inv[i];
    }
  }
  T fact(int n, bool inv = 0) {
    if (inv)
      return Finv[n];
    else
      return Fact[n];
  }
  T inv(int n) { return Inv[n]; }
  T nPr(int n, int r) {
    if (n < 0 || n < r || r < 0)
      return Mint(0);
    else
      return Fact[n] * Finv[n - r];
  }
  T nCr(int n, int r) {
    if (n < 0 || n < r || r < 0)
      return Mint(0);
    else
      return Fact[n] * Finv[r] * Finv[n - r];
  }
};

template <typename T, unsigned p> struct NTT {
  vector<T> rt, irt;
  NTT(int lg = 21) {
    unsigned mod = T::get_mod();
    T prt = p;
    rt.resize(1 << lg, 1);
    irt.resize(1 << lg, 1);
    rep(w, 0, lg) {
      int mask = (1 << w) - 1, t = (mod - 1) >> w;
      T g = prt.pow(t), ig = prt.pow(mod - 1 - t);
      rep(i, 0, mask) {
        rt[mask + i + 1] = g * rt[mask + i];
        irt[mask + i + 1] = ig * irt[mask + i];
      }
    }
  }
  void ntt(vector<T> &f, bool inv = 0) {
    int n = f.size();
    if (inv) {
      for (int i = 1; i < n; i <<= 1)
        for (int j = 0; j < n; j += i * 2)
          rep(k, 0, i) {
            f[i + j + k] *= irt[i * 2 - 1 + k];
            const T tmp = f[j + k] - f[i + j + k];
            f[j + k] += f[i + j + k];
            f[i + j + k] = tmp;
          }
      T mul = T(n).inv();
      rep(i, 0, n) f[i] *= mul;
    } else {
      for (int i = n >> 1; i; i >>= 1)
        for (int j = 0; j < n; j += i * 2)
          rep(k, 0, i) {
            const T tmp = f[j + k] - f[i + j + k];
            f[j + k] += f[i + j + k];
            f[i + j + k] = tmp * rt[i * 2 - 1 + k];
          }
    }
  }
  vector<T> conv(vector<T> a, vector<T> b) {
    int n = a.size() + b.size() - 1,
        m = (n & (n - 1) ? 1 << (32 - __builtin_clz(n)) : n);
    a.resize(m);
    b.resize(m);
    ntt(a);
    ntt(b);
    rep(i, 0, m) a[i] *= b[i];
    ntt(a, 1);
    a.resize(n);
    return a;
  }
};
NTT<Mint, 3> ntt;
vector<Mint> multiply(vector<Mint> a, vector<Mint> b) {
  return ntt.conv(a, b);
};

template <typename T> struct FPS {
  using V = vector<T>;
  V f;
  FPS() {}
  FPS(int _n) : f(_n) {}
  FPS(V _f) { f = _f; }
  T &operator[](const int i) { return f[i]; }
  T eval(Mint x) {
    T res, w = 1;
    for (T v : f)
      res += w * v, w *= x;
    return res;
  }
  int size() const { return f.size(); }
  FPS resize(int n) {
    f.resize(n);
    return *this;
  }
  void shrink() {
    while (!f.empty() and f.back() == 0)
      f.pop_back();
  }
  FPS inv() const {
    assert(f[0] != 0);
    int n = f.size();
    FPS res(n);
    res[0] = f[0].inv();
    for (int k = 1; k < n; k <<= 1) {
      FPS g(k), h(k * 2);
      rep(i, 0, min(k, n)) g[i] = res[i];
      rep(i, 0, min(k * 2, n)) h[i] = f[i];
      g *= g;
      g *= h;
      g.resize(k * 2);
      rep(i, 0, min(k * 2, n)) res[i] = res[i] * 2 - g[i];
    }
    return res;
  }
  FPS operator+(const FPS &g) const { return FPS(*this) += g; }
  FPS operator-(const FPS &g) const { return FPS(*this) -= g; }
  FPS operator*(const FPS &g) const { return FPS(*this) *= g; }
  FPS operator/(const FPS &g) const { return FPS(*this) /= g; }
  FPS operator%(const FPS &g) const { return FPS(*this) %= g; }
  FPS &operator+=(FPS g) {
    if (g.size() > f.size())
      f.resize(g.size());
    rep(i, 0, g.size()) f[i] += g[i];
    shrink();
    return *this;
  }
  FPS &operator-=(FPS g) {
    if (g.size() > f.size())
      f.resize(g.size());
    rep(i, 0, g.size()) f[i] -= g[i];
    shrink();
    return *this;
  }
  FPS &operator*=(FPS g) {
    f = multiply(f, g.f);
    shrink();
    return *this;
  }
  FPS &operator/=(FPS g) {
    if (g.size() > f.size())
      return *this = FPS();
    reverse(ALL(f));
    reverse(ALL(g.f));
    int n = f.size() - g.size() + 1;
    f.resize(n);
    g.resize(n);
    *this *= g.inv();
    f.resize(n);
    reverse(ALL(f));
    shrink();
    return *this;
  }
  FPS &operator%=(FPS g) {
    *this -= *this / g * g;
    shrink();
    return *this;
  }
  FPS diff() {
    FPS res(f.size() - 1);
    rep(i, 0, res.size()) res[i] = f[i + 1] * (i + 1);
    return res;
  }
  FPS inte() {
    FPS res(f.size() + 1);
    for (int i = res.size() - 1; i; i--)
      res[i] = f[i - 1] / i;
    return res;
  }
  FPS log() {
    assert(f[0] == 1);
    int n = f.size();
    FPS res = diff() * inv();
    res = res.inte();
    return res.resize(n);
  }
  FPS exp() {
    assert(f[0] == 0);
    int n = f.size();
    FPS f(1), g(1);
    f[0] = g[0] = 1;
    for (int k = 1; k < n; k <<= 1) {
      g = (g + g - f * g * g).resize(k);
      FPS q = diff().resize(k - 1);
      FPS w = (q + g * (f.diff() - f * q)).resize(2 * k - 1);
      f = (f + f * (*this - w.inte()).resize(2 * k)).resize(2 * k);
    }
    return f.resize(n);
  }
  FPS shift(int c) {
    int n = f.size();
    FPS res = *this, mul(n);
    factorial<T> fact(n + 1);
    mul[1] = c;
    mul = mul.exp();
    rep(i, 0, n) res[i] *= fact.fact(i);
    reverse(ALL(res.f));
    res *= mul;
    res.resize(n);
    reverse(ALL(res.f));
    rep(i, 0, n) res[i] *= fact.fact(i, 1);
    return res;
  }
  FPS pow(ll t) {
    int n = f.size(), k = 0;
    while (k < n and f[k] == 0)
      k++;
    FPS res(n);
    if (t * k >= n)
      return res;
    n -= t * k;
    FPS tmp(n);
    Mint c = f[k], ic = c.inv();
    rep(i, 0, n) tmp[i] = f[i + k] * ic;
    tmp = tmp.log();
    for (auto &x : tmp.f)
      x *= t;
    tmp = tmp.exp();
    c = c.pow(t);
    rep(i, 0, n) res[i + t * k] = tmp[i] * c;
    return res;
  }
};

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  FPS<Mint> f(n + 1);
  for (int i = 0; i < n; ++i)
    f[i].val = 0;
  for (int i = 0; i < m; ++i) {
    int L, R;
    cin >> L >> R;
    for (int j = L; j <= R; ++j) {
      f[j].val = 1;
    }
  }
  auto g = f.pow(n);
  f[0].val = 998244353 - 1;
  g[0].val = 998244353 - 1;
  f = f.inv();
  g = g * f;
  cout << g[n - 1].val << endl;

  return 0;
}