#include <bits/stdc++.h>
using namespace std;
template <class T>
using PQ_inv = priority_queue<T, vector<T>, greater<T>>;
const double pai = 3.1415926535897;
const long long mod = 998244353;
const long long INF = 1000000021;
const long long LINF = 2000000000000000000;
const long long MAX = 510000;
const long long MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (long long i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(long long n, long long k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long PER(long long n, long long k) {
  if (n < 0 || k < 0) return 0;
  if (n < k) return fac[n];
  return fac[n] * finv[n - k] % mod;
}
long long modinv(long long a, long long 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;
}
long long modpow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long fix_mod(long long& xx) {
  if (xx >= mod * 2 || xx < -mod * 2) {
    xx %= mod;
  }
  if (xx >= mod) xx -= mod;
  return xx;
}
struct edge {
  long long to, cost;
};
edge m_e(long long xx, long long yy) {
  edge ed;
  ed.to = xx;
  ed.cost = yy;
  return ed;
}
long long b_s(vector<long long>& vec, long long xx) {
  return lower_bound(vec.begin(), vec.end(), xx) - vec.begin();
}
template <class T>
void vecout(vector<T>& vec) {
  for (T t : vec) cout << t << " ";
  cout << "\n";
}
template <class T>
void vecout(vector<vector<T>>& vec) {
  for (vector<T> tvec : vec) {
    for (T t : tvec) cout << t << " ";
    cout << "\n";
  }
}
template <class T>
void vecout(vector<pair<T, T>>& vec) {
  for (pair<T, T> p : vec) {
    cout << p.first << " " << p.second << "\n";
  }
}
template <class T>
void vecin(vector<T>& vec) {
  for (long long i = 0; i < (long long)vec.size(); i++) cin >> vec[i];
}
bool chmax(long long& xx, long long yy) {
  if (xx < yy) {
    xx = yy;
    return true;
  }
  return false;
}
bool chmin(long long& xx, long long yy) {
  if (xx > yy) {
    xx = yy;
    return true;
  }
  return false;
}
template <class T>
double dist(T x, T y) {
  return sqrt(x * x + y * y);
}
template <class T>
double dist(T xx1, T yy1, T xx2, T yy2) {
  return sqrt((xx1 - xx2) * (xx1 - xx2) + (yy1 - yy2) * (yy1 - yy2));
}
template <class T>
double dist(pair<T, T> p1, pair<T, T> p2) {
  return sqrt((p1.first - p2.first) * (p1.first - p2.first) +
              (p1.second - p2.second) * (p1.second - p2.second));
}
long long gcd(long long xx, long long yy) {
  long long p = xx;
  long long q = yy;
  if (q > p) swap(p, q);
  while (p % q != 0) {
    p %= q;
    swap(p, q);
  }
  return q;
}
long long lcm(long long xx, long long yy) { return (xx / gcd(xx, yy)) * yy; }
bool prime(long long xx) {
  if (xx <= 1) {
    return 0;
  }
  for (long long i = 2; i * i <= xx; i++) {
    if (xx % i == 0) {
      return 0;
    }
  }
  return 1;
}
signed main() {
  long long n, k, t = 0;
  cin >> n >> k;
  vector<pair<long long, long long>> vec(k);
  vector<long long> dp(n + 2);
  dp[1] = 1;
  dp[2] = -1;
  for (long long i = 0; i < (long long)(k); i++) {
    cin >> vec[i].first >> vec[i].second;
  }
  for (long long i = 1; i < n; i++) {
    t += dp[i];
    fix_mod(t);
    for (long long j = 0; j < (long long)(k); j++) {
      if (i + vec[j].first > n) continue;
      if (i + vec[j].second > n) {
        dp[i + vec[j].first] += t;
        fix_mod(dp[i + vec[j].first]);
        dp[n + 1] -= t;
        fix_mod(dp[n + 1]);
      } else {
        dp[i + vec[j].first] += t;
        fix_mod(dp[i + vec[j].first]);
        dp[i + vec[j].second + 1] -= t;
        fix_mod(dp[i + vec[j].second + 1]);
      }
    }
  }
  long long ans = dp[n] + t;
  if (ans < 0) ans += mod;
  cout << ans << "\n";
}
