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

#define F first
#define FF first.first
#define FS first.second
#define S second
#define SF second.first
#define SS second.second
#define ii pair<int,int>
#define iii pair<ii, int>
#define iiii pair<ii, ii>
#define viiii vector< iiii >
#define viii vector< iii >
#define vii vector< ii >
#define vi vector< int >
#define vvi vector<vector< int > >
#define int int64_t


#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define sortv(V) sort(all(V))
#define reversev(V) reverse(all(V))
#define uniquev(V) sortv(V),(V).erase(unique(all(V)),(V).end())

#define gcd(a, b) (__gcd((a), (b)))
#define lcm(a, b) (((a)/__gcd(a, b)) * b)

#define yes cout << "Yes" << endl
#define no cout << "No" << endl

// Trace dinâmico (parameter pack (c++11))
template<typename T>
void println(T a) { cout << a << "\n"; }
template<typename T, typename... Args>
void println(T a, Args... args) { cout << a << " "; println(args...); }

template<typename T>
void print(T a) { cout << a; }
template<typename T, typename... Args>
void print(T a, Args... args) { cout << a << " "; print(args...); }

template<typename T>
void printv(T a, string last="\n") {
    for(auto el: a)
      print(el, "");
    print(last);
}


template<typename T>
void scan(T& a) { cin >> a ; }
template<typename T, typename... Args>
void scan(T& a, Args&&... args) { cin >> a; scan(args...); }

//Functions

int firstGreater(int value, vector<int> &v){
   int l = 0, r = v.size();
   while(l<r){
      int m = (l+r)/2;
      if( v[m]>value ){
         r = m;
      }else{
         l = m+1;
      }
   }

   return l;
}

int firstGreaterOrIgual(int value, vector<int> &v){
   int l = 0, r = v.size();
   while(l<r){
      int m = (l+r)/2;
      if( v[m]>=value ){
         r = m;
      }else{
         l = m+1;
      }
   }
   return l;
}


#define pb push_back
#define FOR(v, i, l) for(int v = i; v < (int)l; ++v)
#define REP(v, l) FOR(v, 0, l)

#define EPS 1e-6
#define oo (1LL<<60)
#define MAX 1000000000000000000LL
#define MOD 998244353LL
#define endl '\n'
#define debug(x) cout << #x << " is " << x << "\n";

const int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const int dy[8] = {2, -2, 2, -2, 1, -1, 1, -2};

//Main
int N, M, K, L, R, J, P, CASES;
int A,B,C;

struct PrefixSumBit{
   int len;
   vector<int> bit;

   /*1 to len*/
   PrefixSumBit(int _len){
      len = _len;
      bit = vector<int>(len+1);
   }
   /*LeastSignificantBit*/
   static int LSB(int value){
      return value & -value;
   }

   void add(int value, int id){
      while( id <= len ){
         bit[id]+=value;
         bit[id]%=MOD;
         id += LSB(id);
      }
   }

   int preSum(int id){
      int ans = 0;
      while(id){
         ans += bit[id];
         ans %= MOD;
         id -= LSB(id);
      }
      return ans;
   }
};

main(){
   std::ios::sync_with_stdio(false);
   cin.tie(0);

   cin >> N >> K;

   vector<ii> S(K);
   for(auto &it: S) cin >> it.F >> it.S;

   PrefixSumBit presum(N);

   presum.add(1, 1);
   for(int i = 2; i <= N; i++){
      for(int k = 0; k < K; k++){
         int r = i-S[k].F;
         if(r>=1){
            int l = max(int(1), i-S[k].S);
            int ans = (presum.preSum(r) - presum.preSum(l-1) + MOD)%MOD;
            presum.add(ans, i);
         }
      }
   }
   println( (presum.preSum(N) - presum.preSum(N-1)+MOD)%MOD );

}
























Measured memory_bytes: 4780