#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long sz = 1e6 + 5;
vector<long long> fac(sz, 1);
long long 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;
}
long long nCr(long long n, long long r) {
  return ((((fac[n] * powmod(fac[n - r], mod - 2)) % mod) *
           powmod(fac[r], mod - 2)) %
          mod);
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long extended_gcd(long long a, long long b, long long &x, long long &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  long long xx, yy;
  long long res = extended_gcd(b, a % b, xx, yy);
  x = yy;
  y = xx - yy * (a / b);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> dp(n + 2, 0);
    dp[1] = 1;
    vector<pair<long long, long long>> arr(k);
    for (int i = 0; i < k; i++) cin >> arr[i].first >> arr[i].second;
    long long sm = 0;
    for (auto p : arr) {
      if (p.first + 1 <= n) {
        dp[1 + p.first] += 1;
        dp[min(n, 1 + p.second) + 1] -= 1;
      }
    }
    for (long long i = 2; i <= n; i++) {
      sm += dp[i];
      if (sm == 0) continue;
      for (auto p : arr) {
        if (i + p.first <= n) {
          dp[i + p.first] += sm;
          dp[min(n, i + p.second) + 1] -= sm;
        }
      }
    }
    for (int i = 3; i <= n; i++) dp[i] = (dp[i] + dp[i - 1]) % mod;
    cout << dp[n] << "\n";
  }
  return 0;
}
