#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cassert>

using namespace std;
typedef long long 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
{
   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, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

class BIT
{
   /*binary indexed tree*/
private:
   vector<mint> data;
   int size;

public:
   BIT(int n) : data(n + 1, 0), size(n)
   {
      return;
   }
   mint sum(int l, int r)
   {
      // return sum in [l+1,r] or (l,r]
      return sum_(r) - sum_(l);
   }
   mint sum_(int i)
   {
      // return sum (0, i]
      mint ans = 0;
      while (0 < i)
      {
         ans += data[i];
         i -= i & -i; // 11010110 -> 1010100
      }
      return ans;
   }
   void add(int i, mint x)
   {
      // add x to i
      while (i <= size)
      {
         data[i] += x;
         i += i & -i;
      }
      return;
   }
};

class BITFILL
{
   /*dimensional binary indexed tree with range update*/
private:
   BIT coefficient;
   BIT constant;

public:
   BITFILL(int n) : coefficient(n), constant(n)
   {
      return;
   }
   mint getSum_(int l)
   {
      // return sum of first l factors, (0,l]
      return coefficient.sum_(l) * l + constant.sum_(l);
   }
   mint getSum(int l, int r)
   {
      // return sum of (l,r]
      return getSum_(r) - getSum_(l);
   }
   void add(int l, int r, mint v)
   {
      // add v in (l,r]
      constant.add(l + 1, -v * (l));
      coefficient.add(l + 1, v);
      constant.add(r, v * r);
      coefficient.add(r, -v);
   }
};

int main()
{
   int N, K;
   cin >> N >> K;
   vector<pair<int, int>> S(K);
   for (int i = 0; i < K; i++)
   {
      cin >> S[i].first >> S[i].second;
   }
   sort(S.begin(), S.end());
   BITFILL bitfill(N);

   bitfill.add(0, 1, 1);
   for (int i = 1; i <= N; i++)
   {
      mint cur = bitfill.getSum(i - 1, i);
      for (int j = 0; j < K; j++)
      {
         if (i + S[j].first - 1 > N)
            break;
         bitfill.add(i + S[j].first - 1, min(i + S[j].second, N), cur);
      }
   }
   cout << bitfill.getSum(N - 1, N);

   return 0;
}


Measured memory_bytes: 6052