#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for (i = s; i < g; i++)
using namespace std;
using ll = long long;
using ss = stringstream;

const ll mod = 1000000000 + 7; // 10^9

const ll llmax = pow(10, 18);

// bool is_integer(double x){
//     return floor(x) == x;
// }

// struct factorial
// {
//     vector<ll> fac, finv, inv;

//     factorial(int n) : fac(2, 1), finv(2, 1), inv(2, 1)
//     {
//         int i;

//         rep(i, 2, n + 1)
//         {
//             fac.push_back(fac[i - 1] * i % mod);
//             inv.push_back(mod - inv[mod % i] * (mod / i) % mod);
//             finv.push_back(finv[i - 1] * inv[i] % mod);
//         }
//     }

//     ll comb(int n, int k)
//     {
//         if (n < k)
//         {
//             return 0;
//         }

//         if (n < 0 || k < 0)
//         {
//             return 0;
//         }

//         return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
//     }

//     ll perm(int n, int k)
//     {
//         if (n < k)
//         {
//             return 0;
//         }

//         if (n < 0 || k < 0)
//         {
//             return 0;
//         }

//         return fac[n] * finv[n - k] % mod;
//     }
// };
// ll power(ll x, ll y)
// {
//     ll ans = 1;
//     while (y)
//     {
//         if (y & 1)
//             ans = (ans * x) % mod;
//         x = (x * x) % mod;
//         y >>= 1;
//     }
//     return ans;
// }

// ll mod_conb(ll n, ll k)
// {
//     ll ans = 1;

//     if (n < k || n < 0 || k < 0)
//     {
//         return 0;
//     }

//     for (long long i = 0; i < k; i++)
//     {
//         ans *= n - i;
//         ans %= mod;
//         ans *= power(i + 1, mod - 2);
//         ans %= mod;
//     }

//     return ans;
// }
// struct unionfind
// {
//     vector<long long> d;

//     unionfind(long long n) : d(n, -1) {}

//     long long root(long long x)
//     {
//         if (d[x] < 0)
//         {
//             return x;
//         }
//         else
//         {
//             return d[x] = root(d[x]);
//         }
//     }

//     bool unite(long long x, long long y)
//     {
//         x = root(x);
//         y = root(y);

//         if (x == y)
//         {
//             return false;
//         }

//         if (d[x] > d[y])
//         {
//             swap(x, y);
//         }

//         d[x] += d[y];
//         d[y] = x;
//         return true;
//     }

//     bool same(long long x, long long y)
//     {
//         return root(x) == root(y);
//     }

//     long long size(long long x)
//     {
//         return -d[root(x)];
//     }
// };

/*逆元　あまりの割り算をするときにこいつをかける(a/b→a*modinv(b))*/
// mod. m での a の逆元 a^{-1} を計算する
// ll modinv(ll a,ll m){
//     long long b = m, u = 1, v = 0;
//     while (b) {
//         long long t = a / b;
//         a -= t * b; swap(a, b);
//         u -= t * v; swap(u, v);
//     }
//     u %= m;
//     if (u < 0) u += m;
//     return u;
// }

// bool IsPrime(ll num)
// {
//     if (num < 2) return false;
//     else if (num == 2) return true;
//     else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

//     double sqrtNum = sqrt(num);
//     for (int i = 3; i <= sqrtNum; i += 2)
//     {
//         if (num % i == 0)
//         {
// 素数ではない
//             return false;
//         }
//     }

// 素数である
//     return true;
// }

/*階乗*/
// ll facctorialMethod(ll k){
//     ll sum = 1;
//     for (ll i = 1; i <= k; ++i)
//     {
//         sum *= i;
// sum%=MOD;//あまりを出力せよ問題の時はこれもやる
//     }
//     return sum;
// }

vector<int> Eratosthenes(ll N) {
  vector<bool> is_prime(N + 1);
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  vector<int> P;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

int main() {
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  vector<ll> L(K);
  vector<ll> R(K);

  ll mll = 998244353;
  vector<ll> DP(N, 0);
  vector<ll> A(N + 1); // i = 0〜i-1番目までのDPが入ってる
  A[0] = 0;
  A[1] = 1;
  DP[0] = 1; // 　0マス目に行くまでの通り

  for (ll i = 0; i < K; i++) {
    cin >> L[i];
    cin >> R[i];
  }

  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      if (i - L[j] < 0)
        continue;

      DP[i] += A[i - L[j] + 1] - A[i - R[j]] + mll;
      DP[i] %= mll;
    }
    A[i + 1] = (A[i] + DP[i]) % mll;
  }

  ll ans = DP[N - 1];

  cout << ans << endl;
}

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for (i = s; i < g; i++)
using namespace std;
using ll = long long;
using ss = stringstream;

const ll mod = 1000000000 + 7; // 10^9

const ll llmax = pow(10, 18);

// bool is_integer(double x){
//     return floor(x) == x;
// }

// struct factorial
// {
//     vector<ll> fac, finv, inv;

//     factorial(int n) : fac(2, 1), finv(2, 1), inv(2, 1)
//     {
//         int i;

//         rep(i, 2, n + 1)
//         {
//             fac.push_back(fac[i - 1] * i % mod);
//             inv.push_back(mod - inv[mod % i] * (mod / i) % mod);
//             finv.push_back(finv[i - 1] * inv[i] % mod);
//         }
//     }

//     ll comb(int n, int k)
//     {
//         if (n < k)
//         {
//             return 0;
//         }

//         if (n < 0 || k < 0)
//         {
//             return 0;
//         }

//         return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
//     }

//     ll perm(int n, int k)
//     {
//         if (n < k)
//         {
//             return 0;
//         }

//         if (n < 0 || k < 0)
//         {
//             return 0;
//         }

//         return fac[n] * finv[n - k] % mod;
//     }
// };
// ll power(ll x, ll y)
// {
//     ll ans = 1;
//     while (y)
//     {
//         if (y & 1)
//             ans = (ans * x) % mod;
//         x = (x * x) % mod;
//         y >>= 1;
//     }
//     return ans;
// }

// ll mod_conb(ll n, ll k)
// {
//     ll ans = 1;

//     if (n < k || n < 0 || k < 0)
//     {
//         return 0;
//     }

//     for (long long i = 0; i < k; i++)
//     {
//         ans *= n - i;
//         ans %= mod;
//         ans *= power(i + 1, mod - 2);
//         ans %= mod;
//     }

//     return ans;
// }
// struct unionfind
// {
//     vector<long long> d;

//     unionfind(long long n) : d(n, -1) {}

//     long long root(long long x)
//     {
//         if (d[x] < 0)
//         {
//             return x;
//         }
//         else
//         {
//             return d[x] = root(d[x]);
//         }
//     }

//     bool unite(long long x, long long y)
//     {
//         x = root(x);
//         y = root(y);

//         if (x == y)
//         {
//             return false;
//         }

//         if (d[x] > d[y])
//         {
//             swap(x, y);
//         }

//         d[x] += d[y];
//         d[y] = x;
//         return true;
//     }

//     bool same(long long x, long long y)
//     {
//         return root(x) == root(y);
//     }

//     long long size(long long x)
//     {
//         return -d[root(x)];
//     }
// };

/*逆元　あまりの割り算をするときにこいつをかける(a/b→a*modinv(b))*/
// mod. m での a の逆元 a^{-1} を計算する
// ll modinv(ll a,ll m){
//     long long b = m, u = 1, v = 0;
//     while (b) {
//         long long t = a / b;
//         a -= t * b; swap(a, b);
//         u -= t * v; swap(u, v);
//     }
//     u %= m;
//     if (u < 0) u += m;
//     return u;
// }

// bool IsPrime(ll num)
// {
//     if (num < 2) return false;
//     else if (num == 2) return true;
//     else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

//     double sqrtNum = sqrt(num);
//     for (int i = 3; i <= sqrtNum; i += 2)
//     {
//         if (num % i == 0)
//         {
// 素数ではない
//             return false;
//         }
//     }

// 素数である
//     return true;
// }

/*階乗*/
// ll facctorialMethod(ll k){
//     ll sum = 1;
//     for (ll i = 1; i <= k; ++i)
//     {
//         sum *= i;
// sum%=MOD;//あまりを出力せよ問題の時はこれもやる
//     }
//     return sum;
// }

vector<int> Eratosthenes(ll N) {
  vector<bool> is_prime(N + 1);
  for (int i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  vector<int> P;
  for (int i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (int j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}

int main() {
  ll N;
  cin >> N;
  ll K;
  cin >> K;
  vector<ll> L(K);
  vector<ll> R(K);

  ll mll = 998244353;
  vector<ll> DP(N, 0);
  vector<ll> A(N + 1); // i = 0〜i-1番目までのDPが入ってる
  A[0] = 0;
  A[1] = 1;
  DP[0] = 1; // 　0マス目に行くまでの通り

  for (ll i = 0; i < K; i++) {
    cin >> L[i];
    cin >> R[i];
  }

  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      if (i - L[j] < 0)
        continue;

      DP[i] += A[i - L[j] + 1] - A[max(i - R[j], ll(0))] + mll;
      DP[i] %= mll;
    }
    A[i + 1] = (A[i] + DP[i]) % mll;
  }

  ll ans = DP[N - 1];

  cout << ans << endl;
}


#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n)                                                        \\
  vector<type> v(n);                                                           \\
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \\
  vector<vector<type>> v(h, vector<type>(w));                                  \\
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)                                                     \\
  std::vector<type> __VA_ARGS__;                                               \\
  MACRO_rdv2_Init(n, __VA_ARGS__);                                             \\
  for (int w_ = 0; w_ < n; ++w_) {                                             \\
    MACRO_rdv2_Scan(w_, __VA_ARGS__);                                          \\
  }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x)                                                                \\
  {                                                                            \\
    wr(x);                                                                     \\
    return;                                                                    \\
  }
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; order_of_key (k) : Number of
// items strictly smaller than k . find_by_order(k) : K-th element in a set
// (counting from zero).
void rset();
void init_test();
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  init_test();
  return 0;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void MACRO_rdv2_Init(int n, T &t) { t.resize(n); }
template <typename First, typename... Rest>
void MACRO_rdv2_Init(int n, First &first, Rest &...rest) {
  first.resize(n);
  MACRO_rdv2_Init(n, rest...);
}
template <typename T> void MACRO_rdv2_Scan(int p, T &t) { std::cin >> t[p]; }
template <typename First, typename... Rest>
void MACRO_rdv2_Scan(int p, First &first, Rest &...rest) {
  std::cin >> first[p];
  MACRO_rdv2_Scan(p, rest...);
}
template <typename T> void wrv(const vector<T> &v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\\n';
}
template <typename T> void wrm(const vector<vector<T>> &v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void wr(const T &x) { cout << x << '\\n'; }
template <typename Head, typename... Tail>
void wr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wr(tail...);
}
template <typename T> void wrf(const T &x) { cout << x << endl; }
template <typename Head, typename... Tail>
void wrf(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wrf(tail...);
}
template <typename T> void debug_out(const T &x) { cerr << x << '\\n'; }
template <typename Head, typename... Tail>
void debug_out(const Head &head, const Tail &...tail) {
  cerr << head << ' ';
  debug_out(tail...);
}
template <typename... T> void err(const T &...cod) {
  wr(cod...);
  exit(0);
}

const int mod = 998244353;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int mult(int x, int y) { return (1LL * x * y) % mod; }

int inv(int x) { return powmod(x, mod - 2); }

int divide(int x, int y) { return mult(x, inv(y)); }

const int N = 5e5 + 10;

void solve() {
  rd(int, n, k);
  vi s;
  rep(i, 0, k) {
    rd(int, l, r);
    per(j, r, l) s.pb(j);
  }

  vi f(n + 1);
  f[1] = 1;
  rep(i, 2, n + 1) {
    for (int x : s) {
      if (i - x >= 1) {
        add_self(f[i], f[i - x]);
      }
    }
  }

  wr(f[n]);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  // cin >> qq;
  while (qq--)
    solve();
}

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int(i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n)                                                        \\
  vector<type> v(n);                                                           \\
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \\
  vector<vector<type>> v(h, vector<type>(w));                                  \\
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)                                                     \\
  std::vector<type> __VA_ARGS__;                                               \\
  MACRO_rdv2_Init(n, __VA_ARGS__);                                             \\
  for (int w_ = 0; w_ < n; ++w_) {                                             \\
    MACRO_rdv2_Scan(w_, __VA_ARGS__);                                          \\
  }
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x)                                                                \\
  {                                                                            \\
    wr(x);                                                                     \\
    return;                                                                    \\
  }
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag,
// tree_order_statistics_node_update> ordered_set; order_of_key (k) : Number of
// items strictly smaller than k . find_by_order(k) : K-th element in a set
// (counting from zero).
void rset();
void init_test();
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  init_test();
  return 0;
}
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> void MACRO_rdv2_Init(int n, T &t) { t.resize(n); }
template <typename First, typename... Rest>
void MACRO_rdv2_Init(int n, First &first, Rest &...rest) {
  first.resize(n);
  MACRO_rdv2_Init(n, rest...);
}
template <typename T> void MACRO_rdv2_Scan(int p, T &t) { std::cin >> t[p]; }
template <typename First, typename... Rest>
void MACRO_rdv2_Scan(int p, First &first, Rest &...rest) {
  std::cin >> first[p];
  MACRO_rdv2_Scan(p, rest...);
}
template <typename T> void wrv(const vector<T> &v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\\n';
}
template <typename T> void wrm(const vector<vector<T>> &v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void wr(const T &x) { cout << x << '\\n'; }
template <typename Head, typename... Tail>
void wr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wr(tail...);
}
template <typename T> void wrf(const T &x) { cout << x << endl; }
template <typename Head, typename... Tail>
void wrf(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  wrf(tail...);
}
template <typename T> void debug_out(const T &x) { cerr << x << '\\n'; }
template <typename Head, typename... Tail>
void debug_out(const Head &head, const Tail &...tail) {
  cerr << head << ' ';
  debug_out(tail...);
}
template <typename... T> void err(const T &...cod) {
  wr(cod...);
  exit(0);
}

const int mod = 998244353;

void add_self(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}

void sub_self(int &x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
}

int add(int x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
  return x;
}

int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += mod;
  return x;
}

int powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res % mod;
}

int mult(int x, int y) { return (1LL * x * y) % mod; }

int inv(int x) { return powmod(x, mod - 2); }

int divide(int x, int y) { return mult(x, inv(y)); }

struct fentree {
  vector<int> bit;
  int n;

  fentree() {
    this->n = 200 * 1000 + 10;
    bit.assign(n, 0);
  }

  fentree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }

  int sum(int r) {
    if (r < 0)
      return 0;
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      add_self(ret, bit[r]);
    return ret;
  }

  void add(int idx, int delta) {
    if (idx < 0)
      return;
    for (; idx < n; idx = idx | (idx + 1))
      add_self(bit[idx], delta);
  }
};

void solve() {
  rd(int, n, k);
  vpii v;
  rep(i, 0, k) {
    rd(int, l, r);
    v.pb(mp(l, r));
  }

  fentree f(n + 1);
  f.add(1, 1);
  rep(i, 2, n + 1) {
    for (auto p : v) {
      f.add(i, f.sum(i - p.ff));
      f.add(i, sub(0, f.sum(i - p.ss - 1)));
    }
  }

  // rep(i, 1, n + 1) dbg(i, f.sum(i) - f.sum(i - 1));

  wr(sub(f.sum(n), f.sum(n - 1)));
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  // cin >> qq;
  while (qq--)
    solve();
}


#include <bits/stdc++.h>

#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc                                                            \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep = "";
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void dbg_out() { cerr << " end.\\n"; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}

const int N = 2e5 + 5;
const int mod = 998244353;

int dp[N];

void solve() {
  int n, k;
  cin >> n >> k;

  vi L(k), R(k);
  vi st;
  for (int i = 0; i < k; ++i) {
    cin >> L[i] >> R[i];
    for (int j = L[i]; j <= R[i]; ++j)
      st.push_back(j);
  }

  sort(All(st));
  memset(dp, 0, sizeof dp);

  dp[1] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int x : st) {
      if (i + x > n)
        break;
      dp[i + x] += dp[i];
      dp[i + x] %= mod;
    }
  }

  cout << dp[n] << "\\n";
}

int main() {
  Fast;
  // louisfghbvc{
  solve();
  //}
  return 0;
}

#include <bits/stdc++.h>

#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc                                                            \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  string sep = "";
  for (const auto &x : v)
    os << sep << x, sep = ", ";
  return os << '}';
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

void dbg_out() { cerr << " end.\\n"; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}

const int N = 2e5 + 5;
const int mod = 998244353;

int dp[N];
int sum[N];

LL get(int l, int r) {
  if (l <= 1)
    l = 1;
  if (l > r)
    return 0;
  return ((sum[r] - sum[l - 1]) % mod + mod) % mod;
}

void solve() {
  int n, k;
  cin >> n >> k;

  vi L(k), R(k);
  vi st;
  for (int i = 0; i < k; ++i) {
    cin >> L[i] >> R[i];
  }

  dp[1] = 1;
  sum[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < k; ++j) {
      dp[i] = (dp[i] + get(i - R[j], i - L[j])) % mod;
    }
    sum[i] = (sum[i - 1] + dp[i]) % mod;
  }

  cout << dp[n] << "\\n";
}

int main() {
  Fast;
  // louisfghbvc{
  solve();
  //}
  return 0;
}


#include <algorithm>
#include <cfenv>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vint vector<ll>
#define vvint vector<vint>
#define P pair<ll, ll>
#define INT_MAX 2147483647
#define MOD 998244353
#define PI 3.14159265358979323846
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;

#define MAX 1000000000000

int main(void) {
  ll n, k;
  cin >> n >> k;
  vint s;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      s.push_back(j);
    }
  }

  vector<ll> dp(n + 2);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    rep(j, s.size()) {
      if (i - s[j] >= 1) {
        dp[i] = (dp[i] + dp[i - s[j]]) % 998244353;
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}

#include <algorithm>
#include <cfenv>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vint vector<ll>
#define vvint vector<vint>
#define P pair<ll, ll>
#define INT_MAX 2147483647
#define MOD 998244353
#define PI 3.14159265358979323846
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;

#define MAX 1000000000000

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> s(k);

  rep(i, k) { cin >> s[i].first >> s[i].second; }

  vector<ll> dp(n + 2);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      if (i - s[j].first >= 1) {
        if (i - s[j].second < 1) {
          dp[i] = (dp[i] + dp[i - s[j].first]) % MOD;
        } else {
          dp[i] = (dp[i] + dp[i - s[j].first] - dp[i - s[j].second - 1]) % MOD;
          if (dp[i] < 0) {
            dp[i] += MOD;
          }
        }
      }
    }
  }

  ll ans = dp[n] - dp[n - 1];
  if (ans < 0) {
    ans += MOD;
  }

  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl "\\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 2e5 + 1;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
struct comp {
  bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const {
    return x.first < y.first;
  }
};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll pwr(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  else
    return pwr(mul(x, x), n / 2);
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}
vector<pair<int, int>> v;
ll dp[200005];
ll solve(int s) {
  // cout << "s " << s << endl;
  if (s == 1)
    return 1;
  else if (dp[s] != -1)
    return dp[s];
  ll ans = 0;

  for (int i = 0; i < v.size() && v[i].first < s; i++) {
    int l = v[i].first, r = v[i].second;
    for (int j = l; j <= r && j < s; j++) {
      ans = (ans + solve(s - j)) % MOD;
    }
  }
  dp[s] = ans;
  return ans;
}
int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k, x, y;
    cin >> n >> k;
    int ans = 0;
    for (int i = 0; i < k; i++) {
      cin >> x >> y;
      v.push_back({x, y});
    }
    sort(v.begin(), v.end());
    memset(dp, -1, sizeof(dp));
    cout << solve(n) << endl;
  }
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl "\\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 2e5 + 1;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
struct comp {
  bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const {
    return x.first < y.first;
  }
};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll pwr(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  else
    return pwr(mul(x, x), n / 2);
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    const int mod = 998244353;
    int n, k;
    cin >> n >> k;
    vector<array<int, 2>> intervals(k);
    for (auto &[x, y] : intervals)
      cin >> x >> y;
    vector<ll> dp(n + 1), pref(n + 1);
    dp[1] = pref[1] = 1;
    for (int i = 2; i <= n; ++i) {
      for (auto [l, r] : intervals) {
        int L = max(1, i - r), R = i - l;
        if (L > R)
          continue;
        // Add dp[L...R]
        dp[i] += (pref[R] - pref[L - 1]) % mod;
        dp[i] = (dp[i] % mod + mod) % mod;
      }
      pref[i] = (pref[i - 1] + dp[i]) % mod;
    }
    cout << dp[n];
  }
}


/*input
60 3
5 8
1 3
10 15
*/

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// from atcoder live
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; ++j) {
      a.push_back(j);
    }
  }
  vector<mint> dp(n);
  dp[0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int d : a) {
      if (i - d < 0)
        continue;
      dp[i] += dp[i - d];
    }
  }
  cout << dp[n - 1] << endl;
}

/*input
60 3
5 8
1 3
10 15
*/

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// from atcoder live
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) cin >> l[i] >> r[i];
  vector<mint> dp(n);
  dp[0] = 1;
  for (int i = 1; i < n; ++i) {
    dp[i] = dp[i - 1];
    rep(j, k) {
      if (i - l[j] < 0)
        continue;
      dp[i] += dp[i - l[j]];
      if (i - r[j] - 1 < 0)
        continue;
      dp[i] -= dp[i - r[j] - 1];
    }
  }
  cout << dp[n - 1] - dp[n - 2] << endl;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  set<int> s;

  rep(j, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; ++i) {
      s.insert(i);
    }
  }

  vector<int> v(s.begin(), s.end());

  vector<ll> dp(n + 1, 0);

  dp[0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int u : v) {
      if (i - u >= 0) {
        dp[i] += dp[i - u];
        dp[i] %= MOD;
      }
    }
  }
  cout << (dp[n - 1] % MOD) << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> l(k, 0), r(k, 0);
  rep(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n, 0), sdp(n + 1, 0);

  dp[0] = 1;
  sdp[1] = 1;

  for (int i = 1; i < n; ++i) {
    rep(j, k) {
      int left = max(0, i - r[j]);
      int right = max(0, i - l[j] + 1);
      ll val = sdp[right] - sdp[left];
      if (val < 0)
        val += MOD;
      dp[i] += val % MOD;
      dp[i] %= MOD;
    }
    sdp[i + 1] = (sdp[i] + dp[i]) % MOD;
  }

  cout << (dp[n - 1] % MOD) << endl;
}


#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

int main() {

  ll mod = 998244353;

  ll N, K;
  cin >> N >> K;

  vector<ll> numbers;

  for (ll i = 0; i < K; i++) {
    ll L, R;
    cin >> L >> R;
    for (ll j = L; j <= R; j++) {
      numbers.push_back(j);
    }
  }

  vector<ll> patterns(N, 0);
  patterns[0] = 1;

  for (ll i = 1; i < N; i++) {
    for (auto n : numbers) {
      if (i - n >= 0) {
        patterns[i] += patterns[i - n];
        patterns[i] %= mod;
      }
    }
  }

  cout << patterns[N - 1] << endl;
  return 0;
}

#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

int main() {

  ll mod = 998244353;

  ll N, K;
  cin >> N >> K;

  /*vector<ll> numbers;

  for (ll i = 0; i < K; i++) {
      ll L, R;
      cin >> L >> R;
      for (ll j = L; j <= R; j++) {
          numbers.push_back(j);
      }
  }*/

  vector<ll> Ls(K), Rs(K);

  for (ll i = 0; i < K; i++) {
    ll L, R;
    cin >> L >> R;
    Ls[i] = L;
    Rs[i] = R;
  }

  vector<ll> patterns(N, 0);
  patterns[0] = 1;

  vector<ll> headIndices(K), tailIndices(K), rangeTotals(K);

  for (ll i = 0; i < K; i++) {
    headIndices[i] = -Rs[i];
    tailIndices[i] = -Ls[i];
    rangeTotals[i] = 0;
  }

  for (ll i = 1; i < N; i++) {
    for (ll j = 0; j < K; j++) {
      if (headIndices[j] >= 0) {
        rangeTotals[j] -= patterns[headIndices[j]];
        rangeTotals[j] += mod;
        rangeTotals[j] %= mod;
      }
      headIndices[j]++;
      tailIndices[j]++;
      if (tailIndices[j] >= 0) {
        rangeTotals[j] += patterns[tailIndices[j]];
      }
      rangeTotals[j] %= mod;
      patterns[i] += rangeTotals[j];
      patterns[i] %= mod;
    }
  }

  /*for (ll i = 1; i < N; i++) {
      for (auto n : numbers) {
          if (i - n >= 0) {
              patterns[i] += patterns[i - n];
              patterns[i] %= mod;
          }
      }
  }*/

  cout << patterns[N - 1] << endl;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

  for (long long i = 0; i < k; i++) {

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\\n';

  return 0;
}

#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[4 * 200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

  for (long long i = 0; i < k; i++) {

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\\n';

  return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, e) for (int i = s; i <= (int)(e); ++i)
using namespace std;
#define int long long int
#define MOD 998244353
//------------ATCODER TEMPLATE-------------

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> v;
  REP(i, K) {
    int L, R;
    cin >> L >> R;
    FOR(j, L, R) { v.push_back(j); }
  }
  sort(v.begin(), v.end());

  vector<int> cnt(N + 1, 0);
  cnt[0] = 1;
  FOR(i, 1, N) {
    for (int x : v) {
      if (i - x < 0)
        break;
      cnt[i] += cnt[i - x];
      cnt[i] %= MOD;
    }
  }
  cout << cnt[N - 1] << endl;
  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, e) for (int i = s; i <= (int)(e); ++i)
using namespace std;
#define int long long int
#define MOD 998244353
//------------ATCODER TEMPLATE-------------

signed main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> ps(K);
  REP(i, K) { cin >> ps[i].first >> ps[i].second; }
  sort(ps.begin(), ps.end());

  vector<int> cnt(N + 1, 0);
  vector<int> S(N + 1, 0);
  S[0] = 0, S[1] = 1;
  ;
  cnt[1] = 1;
  FOR(i, 2, N) {
    for (auto [L, R] : ps) {
      int l = max(i - R - 1, 0LL);
      int r = i - L;
      if (r < 0)
        break;
      cnt[i] = (cnt[i] + S[r] - S[l] + MOD) % MOD;
    }
    S[i] = (S[i - 1] + cnt[i]) % MOD;
  }
  cout << cnt[N] << endl;
  return 0;
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstdint>
#include <iostream>
#include <vector>

#include <cstddef>
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
#define rep(i, n) for (usize i = 0; i < (usize)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;
constexpr i64 MAX = 10000000;
constexpr i64 MOD = 1000000007;
i64 fac[MAX], finv[MAX], inv[MAX];

template <i64 modulus> class modcal {

public:
  i64 a;

  constexpr modcal(const i64 x = 0) noexcept : a(x % modulus) {}

  constexpr i64 &value() noexcept { return a; }

  constexpr const i64 &value() const noexcept { return a; }

  constexpr modcal operator+(const modcal rhs) const noexcept {
    return modcal(*this) += rhs;
  }

  constexpr modcal operator-(const modcal rhs) const noexcept {
    return modcal(*this) -= rhs;
  }

  constexpr modcal operator*(const modcal rhs) const noexcept {
    return modcal(*this) *= rhs;
  }

  constexpr modcal operator/(const modcal rhs) noexcept {
    return modcal(*this) /= rhs;
  }

  constexpr modcal &operator+=(const modcal rhs) noexcept {
    a += rhs.a;
    if (a >= modulus) {
      a -= modulus;
    }
    return *this;
  }

  constexpr modcal &operator-=(const modcal rhs) noexcept {
    if (a < rhs.a) {
      a += modulus;
    }
    a -= rhs.a;
    return *this;
  }

  constexpr modcal &operator*=(const modcal rhs) noexcept {
    a = a * rhs.a % modulus;
    return *this;
  }

  constexpr modcal &operator/=(modcal rhs) noexcept {
    i64 exp = modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }

  void COMninit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i - 1] * i % modulus;
      inv[i] = MOD - inv[modulus % i] * (modulus / i) % modulus;
      finv[i] = finv[i - 1] * inv[i] % modulus;
    }
  }

  i64 COMn(i64 n, i64 k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % modulus) % modulus;
  }

  constexpr modcal<1000000007> modpow(const modcal<1000000007> &a, i64 n) {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }

  i64 modinv(i64 x) { // xy = 1となるyのうちMOD以下になるものを返す
    i64 m = MOD;
    i64 u = 1, v = 0;
    while (m) {
      i64 t = x / m;
      x -= t * m;
      swap(x, m);
      u -= t * v;
      swap(u, v);
    }
    u %= MOD;
    if (u < 0)
      u += MOD;
    return u;
  }
};

using modc = modcal<998244353>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i32 n, k;
  cin >> n >> k;
  vector<i32> imos(n + 1);
  rep(i, k) {
    i32 l, r;
    cin >> l >> r;
    imos[l]++;
    imos[r + 1]--;
  }
  vector<i32> rec;
  rep(i, n) {
    imos[i + 1] += imos[i];
    if (imos[i + 1])
      rec.emplace_back(i + 1);
  }
  vector<vector<modc>> dp(rec.size(), vector<modc>(n + 1));
  rep(i, rec.size()) { dp[i][1] = 1; }
  rep(i, rec.size()) {
    for (i32 j = rec[i] + 1; j <= n; j++)
      if (i == 0) {
        dp[i][j] = dp[i][j - rec[i]];
      } else {
        dp[i][j] = dp[i][j - rec[i]] + dp[i - 1][j - rec[i]] + dp[i - 1][j];
      }
  }
  cout << dp[rec.size()][n].a << endl;
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
#define rep(i, n) for (usize i = 0; i < (usize)(n); i++)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using P = pair<i64, i64>;
constexpr i64 MAX = 10000000;
constexpr i64 MOD = 998244353;
i64 fac[MAX], finv[MAX], inv[MAX];

template <i64 modulus> class modcal {

public:
  i64 a;

  constexpr modcal(const i64 x = 0) noexcept : a(x % modulus) {}

  constexpr i64 &value() noexcept { return a; }

  constexpr const i64 &value() const noexcept { return a; }

  constexpr modcal operator+(const modcal rhs) const noexcept {
    return modcal(*this) += rhs;
  }

  constexpr modcal operator-(const modcal rhs) const noexcept {
    return modcal(*this) -= rhs;
  }

  constexpr modcal operator*(const modcal rhs) const noexcept {
    return modcal(*this) *= rhs;
  }

  constexpr modcal operator/(const modcal rhs) noexcept {
    return modcal(*this) /= rhs;
  }

  constexpr modcal &operator+=(const modcal rhs) noexcept {
    a += rhs.a;
    if (a >= modulus) {
      a -= modulus;
    }
    return *this;
  }

  constexpr modcal &operator-=(const modcal rhs) noexcept {
    if (a < rhs.a) {
      a += modulus;
    }
    a -= rhs.a;
    return *this;
  }

  constexpr modcal &operator*=(const modcal rhs) noexcept {
    a = a * rhs.a % modulus;
    return *this;
  }

  constexpr modcal &operator/=(modcal rhs) noexcept {
    i64 exp = modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }

  void COMninit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i - 1] * i % modulus;
      inv[i] = MOD - inv[modulus % i] * (modulus / i) % modulus;
      finv[i] = finv[i - 1] * inv[i] % modulus;
    }
  }

  i64 COMn(i64 n, i64 k) {
    if (n < k)
      return 0;
    if (n < 0 || k < 0)
      return 0;
    return fac[n] * (finv[k] * finv[n - k] % modulus) % modulus;
  }

  constexpr modcal<998244353> modpow(const modcal<998244353> &a, i64 n) {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }

  i64 modinv(i64 x) { // xy = 1となるyのうちMOD以下になるものを返す
    i64 m = MOD;
    i64 u = 1, v = 0;
    while (m) {
      i64 t = x / m;
      x -= t * m;
      swap(x, m);
      u -= t * v;
      swap(u, v);
    }
    u %= MOD;
    if (u < 0)
      u += MOD;
    return u;
  }
};

using modc = modcal<998244353>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  i64 n, k;
  cin >> n >> k;
  vector<i32> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<modc> dp(n + 1, 0);
  dp[1] = 1;
  vector<modc> imos(n + 1, 0);
  for (i32 i = 1; i <= n; i++) {
    imos[i] = imos[i] + imos[i - 1];
    dp[i] = dp[i] + imos[i];
    rep(j, k) {
      if (l[j] + i <= n)
        imos[l[j] + i] = imos[l[j] + i] + dp[i];
      if (r[j] + i + 1 <= n)
        imos[r[j] + i + 1] = imos[r[j] + i + 1] - dp[i];
    }
  }
  cout << dp[n].a << endl;
}


#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

const int mod = 998244353;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod){};
  mint pow(ll y) const {
    mint res(1);
    mint a(x);
    while (y > 0) {
      if (y & 1)
        res *= a;
      a *= a;
      y >>= 1;
    }
    return res;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator+=(const mint a) noexcept {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) noexcept {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) noexcept {
    (x *= a.x) %= mod;
    return *this;
  }
  mint &operator/=(const mint a) noexcept { return (*this) *= a.inv(); }
  mint operator+(const mint a) const noexcept {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const noexcept {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const noexcept {
    mint res(*this);
    return res *= a;
  }
  mint operator/(const mint a) const noexcept {
    mint res(*this);
    return res /= a;
  }
};

ostream &operator<<(ostream &stream, const mint &n) {
  stream << n.x;
  return stream;
};

struct comb {
  vector<mint> fact, ifact;
  comb(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) const noexcept {
    if (k < 0 || n < k)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

using P = pair<ll, ll>;

mint dp[200005];

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> s;
  REP(i, k) {
    ll l, r;
    cin >> l >> r;
    s.emplace_back(l, r);
  }
  dp[0] = 1;
  REP(i, n) {
    REP(j, k) {
      ll l = s[j].first;
      ll r = s[j].second;
      FORe(p, l, r) {
        if (i + p > n)
          break;
        dp[i + p] += dp[i];
      }
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for (ll i = (k); i < (n); i++)
#define FORe(i, k, n) for (ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for (ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for (ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

const int mod = 998244353;

struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod){};
  mint pow(ll y) const {
    mint res(1);
    mint a(x);
    while (y > 0) {
      if (y & 1)
        res *= a;
      a *= a;
      y >>= 1;
    }
    return res;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator+=(const mint a) noexcept {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) noexcept {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) noexcept {
    (x *= a.x) %= mod;
    return *this;
  }
  mint &operator/=(const mint a) noexcept { return (*this) *= a.inv(); }
  mint operator+(const mint a) const noexcept {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const noexcept {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const noexcept {
    mint res(*this);
    return res *= a;
  }
  mint operator/(const mint a) const noexcept {
    mint res(*this);
    return res /= a;
  }
};

ostream &operator<<(ostream &stream, const mint &n) {
  stream << n.x;
  return stream;
};

struct comb {
  vector<mint> fact, ifact;
  comb(int n) : fact(n + 1), ifact(n + 1) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) const noexcept {
    if (k < 0 || n < k)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

using P = pair<ll, ll>;

mint dp[200005];

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> s;
  REP(i, k) {
    ll l, r;
    cin >> l >> r;
    s.emplace_back(l, ++r);
  }
  dp[0] = 1;
  dp[1] = -1;
  REP(i, n) {
    dp[i + 1] += dp[i];
    REP(j, k) {
      ll l = s[j].first;
      ll r = s[j].second;
      if (i + l <= n)
        dp[i + l] += dp[i];
      if (i + r <= n)
        dp[i + r] -= dp[i];
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

using namespace std;
typedef long long int ll;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 998244353;
struct mint {
  typedef long long ll;
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  bool operator>(const mint a) const {
    mint res(*this);
    return res.x > a.x;
  }
  bool operator<(const mint a) const {
    mint res(*this);
    return res.x < a.x;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

constexpr int premax = 200020;
std::array<mint, premax> f, rf;
bool mymodflag = false;
mint nCk(int n, int k) {
  if (n == k)
    return mint(1);
  if (n < k)
    return mint(0);
  if (!mymodflag) {
    f[0] = 1;
    for (int i = 1; i < premax; i++)
      f[i] = f[i - 1] * i;
    rf[premax - 1] = f[premax - 1].pow(mod - 2);
    for (int i = premax - 2; i >= 0; i--)
      rf[i] = rf[i + 1] * (i + 1);
    mymodflag = true;
  }
  return f[n] * rf[n - k] * rf[k];
}

mint nPk(int n, int k) {
  if (n < k)
    return mint(0);
  if (!mymodflag) {
    f[0] = 1;
    for (int i = 1; i < premax; i++)
      f[i] = f[i - 1] * i;
    rf[premax - 1] = f[premax - 1].pow(mod - 2);
    for (int i = premax - 2; i >= 0; i--)
      rf[i] = rf[i + 1] * (i + 1);
    mymodflag = true;
  }
  return f[n] * rf[n - k];
}

struct LazySegmentTree {
private:
  int n;
  vector<mint> node, lazy;

public:
  LazySegmentTree(vector<mint> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  void eval(int k, int l, int r) {
    if (lazy[k].x != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }

  void add(int a, int b, mint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      mint tmp = r - l;
      lazy[k] += tmp * x;
      eval(k, l, r);
    } else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  mint getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    mint vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    mint vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n, k;
  std::cin >> n >> k;
  LazySegmentTree seg(std::vector<mint>(n + 2, 0));
  seg.add(1, 2, 1);

  std::vector<int> L(k), R(k);
  rep(i, k) std::cin >> L[i] >> R[i];

  for (int i = 1; i <= n; i++) {
    rep(j, k) {
      if (i + L[j] > n)
        continue;
      int right = std::min(n + 1, i + R[j] + 1);
      mint v = seg.getsum(i, i + 1);
      seg.add(i + L[j], right, v);
    }
  }
  mint ans = seg.getsum(n, n + 1);
  std::cout << ans.x << std::endl;

  return 0;
}

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

using namespace std;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 998244353;
struct mint {
  typedef long long ll;
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  bool operator>(const mint a) const {
    mint res(*this);
    return res.x > a.x;
  }
  bool operator<(const mint a) const {
    mint res(*this);
    return res.x < a.x;
  }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

struct FenwickTree {
  vector<mint> v;
  int n;
  FenwickTree(int n) : n(n), v(n) {}
  void add(int i, mint x) {
    i++;
    while (i <= n) {
      v[i - 1] += x;
      i += i & -i;
    }
  }

  mint get(int i) {
    mint r = 0;
    while (i >= 1) {
      r += v[i - 1];
      i -= i & -i;
    }
    return r;
  }
};

struct ExFenwickTree {
  FenwickTree p, q;
  ExFenwickTree(int n) : p(n + 1), q(n + 1) {}
  void add(int l, int r, mint w) {
    p.add(l, mint(-1) * w * l);
    p.add(r, w * r);
    q.add(l, w);
    q.add(r, mint(-1) * w);
  }

  mint get(int r) { return p.get(r) + q.get(r) * r; }
};

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int n, k;
  std::cin >> n >> k;
  ExFenwickTree seg(n + 2);
  seg.add(1, 2, 1);

  std::vector<int> L(k), R(k);
  rep(i, k) std::cin >> L[i] >> R[i];

  for (int i = 1; i <= n; i++) {
    rep(j, k) {
      if (i + L[j] > n)
        continue;
      int right = std::min(n + 1, i + R[j] + 1);
      mint v = seg.get(i + 1) - seg.get(i);
      seg.add(i + L[j], right, v);
    }
  }
  mint ans = seg.get(n + 1) - seg.get(n);
  std::cout << ans.x << std::endl;

  return 0;
}


//
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef pair<int, pint> ppint;
typedef pair<ll, pll> ppll;
typedef vector<int> vint;
typedef vector<ll> vll;

const double pi = 3.141592653589793;
const int INF10 = 1000000001;
const ll INF15 = 1e15 + 1;
const long long INF18 = 1e18 + 1;
// const int mod = 1000000007;
const int mod = 998244353;
const double EPS = 0.00001;

#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (ll)(n); i++)
#define rep2(i, start, end) for (int i = (ll)start; i <= (ll)end; i++)
#define vrep(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (ll)n; i > 0; i--)
#define all(n) n.begin(), n.end()
#define pb push_back
#define debug(x) cerr << #x << ": " << x << '\\n'
#define arep(it, a) for (auto it : a)

struct edge {
  edge(int s, int e, long long c) {
    to = e;
    from = s;
    cost = c;
  }
  edge(int i, long long c = 1) { edge(-1, i, c); }
  int from;
  int to;
  long cost;
};
typedef vector<edge> edges;

// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし

// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は０)(O(log n))
long myPow(long x, long n, long m = mod) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator=(const mint &a) {
    x = a.x;
    return *this;
  }
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
  mint &operator++(int) { return *this += mint(1); }
  mint &operator--(int) { return *this -= mint(1); }
  bool operator!=(const int a) { return this->x != a; }
  bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
  mint parm(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[n - k];
  }
};
template <class T> bool maxin(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool minin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class M, class N> common_type_t<M, N> mygcd(M a, N b) {
  a = abs(a);
  b = abs(b);
  if (a < b)
    return mygcd(b, a);
  M r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
template <class M, class N> common_type_t<M, N> mylcm(M a, N b) {
  return (a / mygcd(a, b)) * b;
}

const int N_MAX = 200005;
int n, k;
vector<pint> lr;
mint dp[N_MAX];

void Main() {
  int x = 0, y = INF10, z = 1;
  // 入力
  cin >> n >> k;
  rep(i, k) {
    cin >> x >> y;
    lr.pb({x, y});
  }
  // 処理
  sort(all(lr));
  dp[1] = 1;
  bool tof = false;
  rep1(i, n - 1) {
    tof = false;
    rep(j, k) {
      rep2(l, lr[j].first, lr[j].second) {
        if (i + l > n) {
          tof = true;
          break;
        }
        dp[i + l] += dp[i];
      }
      if (tof)
        break;
    }
  }
  // 出力
  cout << dp[n] << endl;
}

int main() {
  cin.tie(nullptr);
  cout << fixed << setprecision(12);
  Main();
}

//
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
typedef pair<int, pint> ppint;
typedef pair<ll, pll> ppll;
typedef vector<int> vint;
typedef vector<ll> vll;

const double pi = 3.141592653589793;
const int INF10 = 1000000001;
const ll INF15 = 1e15 + 1;
const long long INF18 = 1e18 + 1;
// const int mod = 1000000007;
const int mod = 998244353;
const double EPS = 0.00001;

#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (ll)(n); i++)
#define rep2(i, start, end) for (int i = (ll)start; i <= (ll)end; i++)
#define vrep(i, n) for (int i = (ll)n - 1; i >= 0; i--)
#define vrep1(i, n) for (int i = (ll)n; i > 0; i--)
#define all(n) n.begin(), n.end()
#define pb push_back
#define debug(x) cerr << #x << ": " << x << '\\n'
#define arep(it, a) for (auto it : a)

struct edge {
  edge(int s, int e, long long c) {
    to = e;
    from = s;
    cost = c;
  }
  edge(int i, long long c = 1) { edge(-1, i, c); }
  int from;
  int to;
  long cost;
};
typedef vector<edge> edges;

// bを何回足せばaを超えるか(O(a/b))
// a+b-1/bとすればよし

// 2進数表示したときの最高桁(O(log n))
int bi_max(long n) {
  int m = 0;
  for (m; (1 << m) <= n; m++)
    ;
  m = m - 1;
  return m;
}
// bi_eに二進数表示したやつを代入(O(log^2 n))
// bitset<N>
// a(n)でnの二進数表示が得られて、a[i]=0or1でi番目のfragが立ってるかわかる x^n
// mod m (nが負の時は０)(O(log n))
long myPow(long x, long n, long m = mod) {
  if (n < 0)
    return 0;
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint &operator=(const mint &a) {
    x = a.x;
    return *this;
  }
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
  mint &operator++(int) { return *this += mint(1); }
  mint &operator--(int) { return *this -= mint(1); }
  bool operator!=(const int a) { return this->x != a; }
  bool operator==(const int a) { return this->x == a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
  vector<mint> fact, ifact;
  combination(int n) : fact(n + 1), ifact(n + 1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i)
      ifact[i - 1] = ifact[i] * i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
  mint parm(int n, int k) {
    if (k < 0 || k > n)
      return 0;
    return fact[n] * ifact[n - k];
  }
};
template <class T> bool maxin(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool minin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class M, class N> common_type_t<M, N> mygcd(M a, N b) {
  a = abs(a);
  b = abs(b);
  if (a < b)
    return mygcd(b, a);
  M r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
template <class M, class N> common_type_t<M, N> mylcm(M a, N b) {
  return (a / mygcd(a, b)) * b;
}

const int N_MAX = 200005;
int n, k;
vector<pint> lr;
mint a[N_MAX], mem[N_MAX];

void Main() {
  int x = 0, y = INF10, z = 1;
  // 入力
  cin >> n >> k;
  rep(i, k) {
    cin >> x >> y;
    lr.pb({x, y});
  }
  // 処理
  sort(all(lr));
  mint X = 0;
  a[1] = 1;
  a[2] = -1;
  rep1(i, n - 1) {
    X = X + a[i];
    rep(j, k) {
      if (i + lr[j].first < N_MAX)
        a[i + lr[j].first] += X;
      if (i + lr[j].second + 1 < N_MAX)
        a[i + lr[j].second + 1] -= X;
    }
  }
  // 出力
  cout << X + a[n] << endl;
}

int main() {
  cin.tie(nullptr);
  cout << fixed << setprecision(12);
  Main();
}


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

const long long mod = 998244353;
vector<long long> sm;
vector<long long> dp(200005, -1);

long long dfs(long long rem) {
  if (rem < 0) {
    return 0;
  }
  if (rem == 0) {
    return 1;
  }
  if (dp[rem] != -1) {
    return dp[rem];
  }
  long long ret = 0;
  for (int i = 0; i < sm.size(); i++) {
    ret += dfs(rem - sm[i]);
    ret %= mod;
  }
  dp[rem] = ret;
  return ret;
}

int main() {
  long long n;
  int k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    long long l, r;
    cin >> l >> r;
    while (l <= r) {
      sm.push_back(l++);
    }
  }

  cout << dfs(n - 1) << endl;
  return 0;
}

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

const long long mod = 998244353;

int main() {
  long long n;
  int k;
  cin >> n >> k;
  vector<pair<int, int>> range;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    range.push_back({l, r + 1});
  }

  vector<long long> dp(n);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    if (i > 1) {
      dp[i] += dp[i - 1];
      dp[i] %= mod;
    }
    for (int j = 0; j < k; j++) {
      int l, r;
      tie(l, r) = range[j];
      if (i + l < n) {
        dp[i + l] += dp[i];
        dp[i + l] %= mod;
      }
      if (i + r < n) {
        dp[i + r] -= dp[i];
        dp[i + r] += mod;
        dp[i + r] %= mod;
      }
    }
  }

  cout << dp[n - 1] << endl;
  return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define pb push_back
#define input(arr, n)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
const int mod = 998244353;
ll power(ll a, ll b) {
  ll r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
using namespace std;

map<int, int> m;
int count(int i, int j, int n, vector<int> &v) {
  if (i == n)
    return 1;
  if (i > n || j == int(v.size()))
    return 0;
  if (m.count(i))
    return m[i];
  for (int i1 = 0; i1 < v.size(); i1++)
    m[i] = (m[i] + count(i + v[i1], i1, n, v)) % mod;
  return m[i];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0, j, k1; i < k; i++) {
    cin >> j >> k1;
    for (int j1 = j; j1 <= k1; j1++)
      v.pb(j1);
  }
  cout << count(1, 0, n, v) << '\\n';
  // for(auto it:m)
  // 	cout<<it.first.first<<' '<<it.first.second<<' '<<it.second<<'\\n';
  return 0;
}

#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define pb push_back
#define input(arr, n)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
const int mod = 998244353;
ll power(ll a, ll b) {
  ll r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ar<ll, 2>> v;
  ll dp[n + 2] = {0};
  dp[1] = 1;
  for (int i = 0, j, k1; i < k; i++) {
    cin >> j >> k1;
    v.pb({j, k1});
  }
  for (int i = 1; i <= n; i++) {
    if (i > 2)
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 0; j < k; j++) {
      if (i + v[j][0] <= n) {
        dp[i + v[j][0]] = (dp[i + v[j][0]] + dp[i]) % mod;
        dp[min(i + v[j][1] + 1, n + 1)] =
            ((dp[min(i + v[j][1] + 1, n + 1)] - dp[i]) % mod + mod) % mod;
      }
    }
  }
  cout << dp[n] << '\\n';
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 998244353;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll sq[1000010];

int main() {
  int N, K;
  set<int> st;
  cin >> N >> K;
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      st.insert(i);
  }

  sq[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (auto itr : st) {
      sq[i + itr] += sq[i];
      sq[i] %= mod;
    }
  }

  cout << sq[N] << endl;

  //    for(auto itr : st) cout << itr << endl;

  return 0;
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
// ceil(a/b)   (a + (b - 1))/ b
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1000000007;
const ll mod = 998244353;
const double PI = 3.14159265358979323846;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

ll l[1000010];
ll r[1000010];

int main() {
  int N, K;
  vector<pair<ll, ll>> v(10);
  cin >> N >> K;
  v.resize(K);
  rep(i, K) {
    ll l, r;
    cin >> l >> r;
    v[i] = make_pair(l, r);
  }

  ll cuml = 0;
  ll cumr = 0;
  for (auto itr : v) {
    l[1 + itr.first] += 1;
    r[1 + itr.second + 1] += 1;
  }

  //    rep(i,N) {cout << l[i+1] - r[i+1] << endl;}

  for (int i = 1; i <= N; i++) {
    cuml += l[i];
    cumr += r[i];
    cuml %= mod;
    cumr %= mod;
    ll tmp;
    if (cuml - cumr < 0)
      tmp = mod + cuml - cumr;
    else
      tmp = cuml - cumr;

    for (auto itr : v) {
      l[i + itr.first] += tmp;
      r[i + itr.second + 1] += tmp;
    }
    ///	printf("i:%d cuml:%lld cumr %lld\\n",i, cuml, cumr);
  }
  ll ans = cuml - cumr;
  if (ans < 0)
    cout << ans + mod << endl;
  else
    cout << ans << endl;
  return 0;
}