#include<bits/stdc++.h>
#define fint register int
#define h 5001
#define p 347589
#define mods 998244353
#define int long long
using namespace std;
int f[p],s[p],l[p],r[p];
signed main()
{
	int n,k;
	cin>>n>>k;
	for(fint i=1;i<=k;i++)
	cin>>l[i]>>r[i];
	f[1]=1,s[1]=1;
	for(fint i=2;i<=n;i++)
	{
		for(fint j=1;j<=k;j++)
		f[i]=f[i]-(s[(i-r[j]-1<0?0:i-r[j]-1)]-s[i-l[j]<0?0:i-l[j]]),f[i]%=mods;
		s[i]=s[i-1]+f[i],s[i]%=mods;
	}
	cout<<(f[n]+mods)%mods;
	return 0;
}