#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long 
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;  
#define mod 998244353
#define fi first
#define sec second
int main()
{
	ll n,k;
	cin>>n>>k;
	ll x,y;
	pair<ll,ll> p[k];
	for(ll i=0;i<k;i++)
	{
		cin>>p[i].fi>>p[i].sec;
	}
	ll dp[n+1]={0};
	ll suf[n+1]={0};
	dp[n]=1;
	suf[n]=1;
	for(ll i=n-1;i>=1;i--)
	{
		for(ll j=0;j<k;j++)
		{
			if(i+p[j].fi<=n)
			{
				if(i+p[j].sec<=n)
				{
					dp[i]=(dp[i]+suf[i+p[j].fi]-suf[i+p[j].sec]+dp[i+p[j].sec])%mod;
				}
				else
				{	
					dp[i]=(dp[i]+suf[i+p[j].fi]-suf[n]+dp[n])%mod;
				}
			}
		}
		suf[i]=(suf[i]+suf[i+1]+dp[i])%mod;
	}
	cout<<dp[1];
}