CodeEff/ECCO huggingface dataset:
# Original solution
N,K = [int(i) for i in input().split()]
mod = 998244353
LR = []
S = []
dp = [0]*(N+1)
dps = [0]*(N+1)
dps[1] = 1
for i in range(K):
  LR.append([int(i) for i in input().split()])
rM = 0
for i in range(N):
  for lr in LR:
    if lr[0] <= i and i <= lr[1]:
      S.append(i)
  rM = max(rM, lr[1])
for i in range(1,N):
  if i in S:
    dp[i] = 1
  for l,r in LR:
    l,r = i - r,i - l
    if r < 1:
      continue
    l = max(0,l)
    dp[i] += dps[r] - dps [l-1]
    dp[i] %= mod
    dps[i] += dps[r] - dps[l]
    
  dps[i] = dps[i-1] + dp[i]
print((dp[-2]))

# Optimized solution
N,K = [int(i) for i in input().split()]
mod = 998244353
LR = []
S = []
dp = [0]*(N+1)
dps = [0]*(N+1)
dps[1] = 1
dp[1] = 1
for i in range(K):
  LR.append([int(i) for i in input().split()])

for i in range(1,N+1):
  for l,r in LR:
    l,r = i - r,i - l
    #print(l,r)
    if r < 1:
      continue
    l = max(1,l)
    dp[i] += dps[r] - dps[l-1]
    dp[i] %= mod
    #print(dp[i])
    
  dps[i] = dps[i-1] + dp[i]
  #print(dp, dps)
print((dp[-1]))
# Original solution
import sys
input = sys.stdin.readline
INF = 10**18
sys.setrecursionlimit(10**6)
import itertools as it
import collections as cl
from collections import deque
import math
from functools import reduce
from collections import defaultdict
MOD = 998244353


def li():
    return [int(x) for x in input().split()]

N, K = li()

s = set()
for _ in range(K):
    a, b = li()
    for i in range(a, b+1):
        s.add(i)

# dp[i]: マスiに到達する方法の個数
dp = [0] * (N+1)
dp[0] = 1
for i in range(N):
    for j in s:
        if i + j <= N:
            dp[i+j] += dp[i]
            dp[i+j] %= MOD
print((dp[N-1]))



# Optimized solution
import sys
input = sys.stdin.readline
INF = 10**18
sys.setrecursionlimit(10**6)
import itertools as it
import collections as cl
from collections import deque
import math
from functools import reduce
from collections import defaultdict
MOD = 998244353


def li():
    return [int(x) for x in input().split()]

N, K = li()

L, R = [], []
for _ in range(K):
    l, r = li()
    L.append(l)
    R.append(r)

# dp[i]: i番目のマスにいく場合の数
dp = [0] * (N+1)
dp[1] = 1
# 区間 [left, right)の和はdpsum[right] - dpsum[left]
# 区間 [left, right]の和はdpsum[right+1] - dpsum[left]
# マス a~bは区間だと[a-1,b-1]に対応。よってマスliからriの和はdpsum[right] - dpsum[left - 1]
dpsum = [0] * (N+1)
dpsum[1] = 1
for i in range(2, N+1):
    for j in range(K):
        li = max(i - R[j], 1)
        ri = i - L[j]
        if ri >= 0:
            dp[i] += dpsum[ri] - dpsum[li-1]
            dp[i] %= MOD
    dpsum[i] = dpsum[i-1] + dp[i]
    dpsum[i] %= MOD

print((dp[N]))
# Original solution
N,K = list(map(int, input().split()))
d = [0]*2*K
for i in range(K):
    d[2*i], d[2*i+1] = list(map(int, input().split()))
a=[0]*(N+1)
b=[0]*(N+1)
a[1]=1
b[1]=1
for i in range(2,N+1):
    t=0
    for j in range(K):
        if i-d[2*j]>0:
            t+=b[i-d[2*j]]
        if i-d[2*j+1]-1>0:
            t-=b[i-d[2*j+1]-1]
    a[i]=t
    b[i]=b[i-1]+a[i]
print((a[N]%998244353))

# Optimized solution
N,K = list(map(int, input().split()))
d = [0]*2*K
for i in range(K):
    d[2*i], d[2*i+1] = list(map(int, input().split()))
a=[0]*(N+1)
b=[0]*(N+1)
a[1]=1
b[1]=1
for i in range(2,N+1):
    t=0
    for j in range(K):
        if i-d[2*j]>0:
            t+=b[i-d[2*j]]
        if i-d[2*j+1]-1>0:
            t-=b[i-d[2*j+1]-1]
    a[i]=t
    b[i]=(b[i-1]+a[i])%998244353
print((a[N]%998244353))
# Original solution
n,k=list(map(int,input().split()))
lst=[list(map(int,input().split())) for i in range(k)]
mod=998244353


dp=[1]
imos=[0]*(2*n+10)
imos[0]=1
imos[1]=-1
for i in range(n):
    for l,r in lst:
        imos[i+l]+=dp[-1]
        imos[i+r+1]-=dp[-1]
    dp.append((dp[-1]+imos[i+1])%mod)


print((dp[n-1]))

# Optimized solution
n,k=list(map(int,input().split()))
lst=[list(map(int,input().split())) for i in range(k)]
mod=998244353

dp=[0]*(2*n+10)
dp[0]=1
dp[1]=-1
for i in range(n):
    for l,r in lst:
        dp[i+l]+=dp[i]
        dp[i+r+1]-=dp[i]
    
    dp[i+1]+=dp[i]
    dp[i+1]%=mod


print((dp[n-1]))
# Original solution
n, k = list(map(int, input().split()))
mod = 998244353
list_s = []
for _ in range(k):
    l, r = list(map(int, input().split()))
    list_s.append((l, r))
dp = [0] * (n + 1)
sdp = [0] * (n + 1)
dp[1] = 1
sdp[0] = 0
sdp[1] = 1

for i in range(2, n + 1):
    for l, r in list_s:
        dl = max(0, i - r - 1)
        dr = max(0, i - l)
        dp[i] += sdp[dr] - sdp[dl]
        sdp[i] = sdp[i - 1] + dp[i]
print((dp[n] % mod))

# Optimized solution
n, k = list(map(int, input().split()))
mod = 998244353
list_s = []
for _ in range(k):
    l, r = list(map(int, input().split()))
    list_s.append((l, r))
dp = [0] * (n + 1)
sdp = [0] * (n + 1)
dp[1] = 1
sdp[0] = 0
sdp[1] = 1

for i in range(2, n + 1):
    for l, r in list_s:
        dl = max(0, i - r - 1)
        dr = max(0, i - l)
        dp[i] += sdp[dr] - sdp[dl]
        sdp[i] = (sdp[i - 1] + dp[i]) % mod
print((dp[n] % mod))
# Original solution
def main():
    n, k = list(map(int, input().split()))
    ds = set()
    for _ in range(k):
        l, r = list(map(int, input().split()))
        ds = ds | {i for i in range(l, r + 1)}
    dp = [0] * (n + 1)
    dp[1] = 1

    for i in range(1, n + 1):
        for d in ds:
            if i + d <= n:
                dp[i + d] = (dp[i + d] + dp[i]) % 998244353

    print((dp[-1] % 998244353))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    lr = [list(map(int, input().split())) for _ in range(k)]
    dp = [0] * (n + 1)
    dp[1] = 1
    dpsum = [0] * (n + 1)
    dpsum[1] = 1
    mod = 998244353

    for i in range(2, n + 1):
        for j in range(k):
            li = i - lr[j][1]
            ri = i - lr[j][0]
            if ri < 0:
                continue
            dp[i] = (dp[i] + dpsum[ri] - dpsum[li - 1]) % mod
        dpsum[i] = dpsum[i - 1] + dp[i]

    print((dp[-1] % mod))


if __name__ == "__main__":
    main()
# Original solution
n, k = list(map(int, input().split()))
lr = []
for _ in range(k):
    lr.append(tuple(map(int, input().split())))
mod = 998244353

dp = [0]*n
dp[0] = 1

acc = [0]*n
acc[0] = 1

for i in range(1, n):
    for l, r in lr:
        if 0 <= i-r and i-l < i:
            dp[i] += acc[i-l]-acc[i-r-1]
        elif i-r < 0 and i-l < i:
            dp[i] += acc[i-l]
        elif i-r < 0 and i <= i-l:
            dp[i] += acc[i-1]
        elif 0 < i-r and i <= i-l:
            dp[i] += acc[i-1]-acc[i-r-1]
    acc[i] = acc[i-1]+dp[i]

print((dp[-1]%mod))

# Optimized solution
n, k = list(map(int, input().split()))
lr = []
for _ in range(k):
    lr.append(tuple(map(int, input().split())))
mod = 998244353

dp = [0]*n
dp[0] = 1

acc = [0]*n
acc[0] = 1

for i in range(1, n):
    for l, r in lr:
        if 0 <= i-r and i-l < i:
            dp[i] += acc[i-l]-acc[i-r-1]
        elif i-r < 0 and i-l < i:
            dp[i] += acc[i-l]
        elif i-r < 0 and i <= i-l:
            dp[i] += acc[i-1]
        elif 0 < i-r and i <= i-l:
            dp[i] += acc[i-1]-acc[i-r-1]
    acc[i] = acc[i-1]+dp[i]%mod

print((dp[-1]%mod))
# Original solution
N, K = list(map(int, input().split()))
MOD = 998244353
P = []
for _ in range(K):
    l, r = list(map(int, input().split()))
    for i in range(l, r+1):
        P.append(i)
dp = [0]*(N+1)
dp[0]=1
for i in range(N):
    for p in P:
        dp[min(i+p,N)] += dp[i]
        dp[min(i + p, N)]%=MOD
print((dp[N-1]))

# Optimized solution
N, K = list(map(int, input().split()))
MOD = 998244353
D = []
for _ in range(K):
    l, r = list(map(int, input().split()))
    D.append((l,r))
imos = [0]*(N+2)
dp = [0]*(N+1)
imos[0]=1
imos[1]=-1
for i in range(N):
    dp[i] = dp[i-1] + imos[i]
    dp[i] %= MOD
    for a, b in D:
        imos[min(i+a,N+1)] += dp[i]
        imos[min(i+b+1,N+1)] -= dp[i]

print((dp[N-1]))
# Original solution
mod = 998244353
n,k = list(map(int,input().split()))
K = [list(map(int,input().split())) for i in range(k)]

dp = [0]*(n+5)
dp[1] = 1
dp2 = [0]*(n+5)
for i in range(1,n+1):
    dp[i] = (dp[i]+dp2[i]+dp2[i-1])%mod
    dp2[i] = (dp2[i]+dp2[i-1])%mod
    for l,r in K:
        dp2[min(i+l,n+2)] += dp[i]
        dp2[min(i+l,n+2)] %= mod
        dp2[min(i+r+1,n+2)] -= dp[i]
        dp2[min(i+r+1,n+2)] %= mod
print((dp[n]%mod))


# Optimized solution
mod = 998244353
n,k = list(map(int,input().split()))
K = [list(map(int,input().split())) for i in range(k)]

dp = [0]*(n+1)
dp[0] = 1
dp2 = [0]*(n+1)
for i in range(n):
    dp2[i] = (dp2[i]+dp2[i-1])%mod
    dp[i] = (dp[i]+dp2[i])%mod
    for l,r in K:
        dp2[min(i+l,n)] = (dp2[min(i+l,n)]+dp[i])%mod
        dp2[min(i+r+1,n)] = (dp2[min(i+r+1,n)]-dp[i])%mod
print((dp[n-1]%mod))
# Original solution
n,k=list(map(int,input().split()))
dp=[0 for i in range(n+1)]
dp[1]=1
accum=[0 for i in range(n+1)]
accum[1]=1
s=[]
for i in range(k):
    x,y=list(map(int,input().split()))
    s.append([x,y])
for i in range(2,n+1):
    for j in s:
        l,r=j
        tmpl=i-r
        tmpr=i-l
        if(tmpl<1):
            tmpl=1
        if(tmpr>=i):
            tmpr=i-1
        if(tmpr<0):
            continue
        dp[i]+=accum[tmpr]-accum[tmpl-1]
    accum[i]=dp[i]+accum[i-1]
print((dp[n]%998244353))

# Optimized solution
n,k=list(map(int,input().split()))
dp=[0 for i in range(n+1)]
dp[1]=1
accum=[0 for i in range(n+1)]
accum[1]=1
s=[]
for i in range(k):
    x,y=list(map(int,input().split()))
    s.append([x,y])
for i in range(2,n+1):
    for j in s:
        l,r=j
        tmpl=i-r
        tmpr=i-l
        if(tmpl<1):
            tmpl=1
        if(tmpr>=i):
            tmpr=i-1
        if(tmpr<0):
            continue
        dp[i]+=accum[tmpr]-accum[tmpl-1]
        dp[i]%=998244353
    accum[i]=dp[i]+accum[i-1]
    accum[i]%=998244353
print((dp[n]%998244353))
# Original solution
N, K = list(map(int, input().split()))

L = [0] * K
R = [0] * K
for i in range(0, K):
    L[i], R[i] = list(map(int, input().split()))

moves = [0] * N
moves[0] = 1

rui_wa = [0] * N
rui_wa[0] = 1

for i in range(1, N):
    for j in range(0, K):
        l = max(i - L[j], 0)
        r = max(i - R[j], 0)
        if i - L[j] < 0:
            continue

        moves[i] += rui_wa[l] - rui_wa[r - 1]

    rui_wa[i] = moves[i] + rui_wa[i - 1]



print((moves[N - 1] % 998244353))


# Optimized solution
N, K = list(map(int, input().split()))

L = [0] * K
R = [0] * K
for i in range(0, K):
    L[i], R[i] = list(map(int, input().split()))

moves = [0] * N
moves[0] = 1

rui_wa = [0] * N
rui_wa[0] = 1

for i in range(1, N):
    for j in range(0, K):
        l = max(i - L[j], 0)
        r = max(i - R[j], 0)
        if i - L[j] < 0:
            continue

        moves[i] += (rui_wa[l] - rui_wa[r - 1]) % 998244353

    rui_wa[i] = (moves[i] + rui_wa[i - 1])  % 998244353



print((moves[N - 1] % 998244353))
# Original solution
import sys
import math


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = list(map(int, input().split()))
    L = [[] for _ in range(K)]
    for t in range(K):
        l, r = list(map(int, input().split()))
        for j in range(l, r + 1):
            L[t].append(j)

    lc = [0] * K
    mod = 998244353

    dp = [0] * (N + 1)
    dp[0] = 1
    for i in range(1,N+1):

        for k in range(K):
            CL =L[k]
            if len(CL)==1:
                lc[k]=dp[i-CL[0]]
            else:
                if CL[-1] > i:
                    lc[k] = lc[k] +dp[i-L[k][0]]
                else:
                    lc[k] =( lc[k] + dp[i - L[k][0]] - dp[i - L[k][-1]-1] )%mod
            dp[i] += lc[k]



    print((dp[N - 1]%mod))


if __name__ == "__main__":
    main()

# \n


# Optimized solution
import sys
import math


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = list(map(int, input().split()))
    L = [[] for _ in range(K)]
    for t in range(K):
        l, r = list(map(int, input().split()))
        for j in range(l, r + 1):
            L[t].append(j)

    lc = [0] * K
    mod = 998244353

    dp = [0] * (N + 1)
    dp[0] = 1
    for i in range(1,N+1):

        for k in range(K):
            CL =L[k]
            if len(CL)==1:
                lc[k]=dp[i-CL[0]] %mod
            else:
                if CL[-1] > i:
                    lc[k] = lc[k] %mod +dp[i-L[k][0]] %mod
                else:
                    lc[k] =( lc[k] + dp[i - L[k][0]] - dp[i - L[k][-1]-1] )%mod
            dp[i] += lc[k]



    print((dp[N - 1]%mod))


if __name__ == "__main__":
    main()

# \n
# Original solution
mod = 998244353
N,K = list(map(int,input().split()))
move = [list(map(int,input().split())) for _ in range(K)]

dp = [0]*(N+1)
dp_sum = [0]*(N+1)
dp[1] = dp_sum[1] = 1
for i in range(2,N+1):
    for l,r in move:
        if 0<i-l:
            dp[i] += dp_sum[i-l]
            if 0<i-r-1:
                dp[i] -= dp_sum[i-r-1]
    dp_sum[i] = dp_sum[i-1] + dp[i]
            
print((dp[-1]%mod))

# Optimized solution
mod = 998244353
N,K = list(map(int,input().split()))
move = [list(map(int,input().split())) for _ in range(K)]

dp = [0]*(N+1)
dp_sum = [0]*(N+1)
dp[1] = dp_sum[1] = 1
for i in range(2,N+1):
    for l,r in move:
        if 0<i-l:
            dp[i] += dp_sum[i-l]
            if 0<i-r-1:
                dp[i] -= dp_sum[i-r-1]
        dp[i] %= mod
    dp_sum[i] = dp_sum[i-1] + dp[i]
            
print((dp[-1]%mod))
# Original solution
import sys
import re
import queue
import collections
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(eval(input()))
def i_map(): return list(map(int, input().split()))
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return eval(input())
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []


def main():
	N,K= i_map()
	L = []
	R = []
	for i in range(0,K):
		tmpL,tmpR = i_map()
		L.append(tmpL)
		R.append(tmpR)

	a = []
	a.append(0)
	a.append(-1)
	for i in range(2,3*N):
		a.append(0)

	f = []
	f.append(1)

	for i in range(0,N):
		for j in range(0,K):
			a[i+L[j]] += f[i]
			a[i+R[j]+1] += -f[i]
		f.append(f[i]+a[i+1])

	print((f[N-1]%998244353))

	return

if __name__ == '__main__':
	main()

# Optimized solution
import sys
import re
import queue
import collections
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(eval(input()))
def i_map(): return list(map(int, input().split()))
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return eval(input())
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []


def main():
	N,K= i_map()
	m = 998244353
	L = []
	R = []
	for i in range(0,K):
		tmpL,tmpR = i_map()
		L.append(tmpL)
		R.append(tmpR)

	a = []
	a.append(0)
	a.append(-1)
	for i in range(2,3*N):
		a.append(0)

	f = []
	f.append(1)

	for i in range(0,N):
		for j in range(0,K):
			a[i+L[j]] += f[i]
			a[i+R[j]+1] += -f[i]
		f.append((f[i]+a[i+1])%m)

	print((f[N-1]%m))

	return

if __name__ == '__main__':
	main()
# Original solution
N, K = list(map(int, input().split()))

LR = [list(map(int, input().split())) for _ in range(K)]
MOD = 998244353
dic = []
for l, r in LR:
    tmp = [x for x in range(l, r+1)]
    dic += tmp
dic.sort()
dp = [0]*(N+1)
dp[1] = 1
n = len(dic)
cnt = 0
t = []
for i in range(2, N+1):
    tmp = i-1
    while cnt < n:
        if dic[cnt] <= tmp:
            t.append(dic[cnt])
            cnt += 1
        else:
            break
    for s in t:
        dp[i] += dp[i-s]
        dp[i] %= MOD
print((dp[-1]))


# Optimized solution
def main():
    N, K = list(map(int, input().split()))

    LR = [list(map(int, input().split())) for _ in range(K)]
    MOD = 998244353

    dp = [0]*(N+1)
    S = [0]*(N+1)
    dp[1] = 1
    S[1] = 1

    for i in range(2, N+1):
        for l, r in LR:
            if i-l < 0:
                continue
            else:
                dp[i] += S[i-l] - S[max(i-r-1, 0)]
        dp[i] %= MOD
        S[i] = S[i-1] + dp[i]
    print((dp[-1]%MOD))

if __name__ == '__main__':
    main()
# Original solution
MOD = 998244353

class BIT:
    def __init__(self, n, init=None):
        self.size = n
        if init is None:
            self.v = [0] * (n + 1) # don't use bit[0]
        else:
            self.v = [0] + init
            for i in range(1, n):
                if i + (i & -i) <= n: self.v[i + (i & -i)] += self.v[i]

    def sum(self, i):
        '''
        Calculate a[1] + ... + a[i]
        '''
        S = 0
        while i > 0:
            S += self.v[i]
            i -= i & -i
        return S
    
    def modsum(self, i, mod):
        '''
        Calculate a[1] + ... + a[i]
        '''
        S = 0
        while i > 0:
            S += self.v[i]
            S %= mod
            i -= i & -i
        return S


    def add(self, i, x):
        '''
        set a[i] to be a[i] + x
        '''
        assert i > 0
        while i <= self.size:
            self.v[i] += x
            i += i & -i

N, K = list(map(int, input().split()))
segments = [tuple(map(int, input().split())) for _ in range(K)]
bit = BIT(N + 1)
bit.v[1] = 1
for i in range(1, N + 1):
    temp = bit.sum(i)
    for s, t in segments:
        if i + s <= N:
            bit.add(i + s, temp)
            bit.add(min(i + t + 1, N + 1), -temp)
print((bit.modsum(N, MOD)))

# Optimized solution
MOD = 998244353

N, K = list(map(int, input().split()))
segments = [tuple(map(int, input().split())) for _ in range(K)]
dp = [0] * (2 * N + 1)
for i in range(N):
    for s, t in segments:
        t += 1
        dp[i + s] += dp[i] if i != 0 else 1
        dp[i + t] -= dp[i] if i != 0 else 1
    dp[i + 1] += dp[i]
    dp[i + 1] %= MOD
print((dp[N - 1]))
# Original solution
N, K = (int(i) for i in input().split())
l, r = [0]*K, [0]*K
for k in range(K):
    l[k], r[k] = (int(x) for x in input().split())

# dp : マスiまで移動する方法のパターン数
# TLE
'''
import numpy as np
dp = [0]*N
dp[0] = 1
for i in range(N):
    for k in range(K):
        for j in range(l[k], r[k]+1):
            if (i+j < N):
                dp[i+j] = dp[i+j] + dp[i]
            if j == r[k]:
                print(i, (l[k], r[k]), dp, np.diff(dp))
print(dp[-1]%998244353)
'''

# dp : マスiまで移動する方法のパターン数
# dps : dp[i] と dp[i+1] の 差
dp = [0]*N
dps = [0]*(N-1)
dp[0] = 1
dps[0] = -1
for i in range(N-1):
    for k in range(K):
        if i + l[k] - 1 < (N - 1):
            dps[i + l[k] - 1] += dp[i]
        if i + r[k] < (N - 1):
            dps[i + r[k]    ] -= dp[i]
        #print(i, (l[k], r[k]), dp, dps)
    dp[i+1] = dps[i] + dp[i]
print((dp[-1]%998244353))

# Optimized solution
N, K = (int(i) for i in input().split())
l, r = [0]*K, [0]*K
for k in range(K):
    l[k], r[k] = (int(x) for x in input().split())

# dp : マスiまで移動する方法のパターン数
# TLE
'''
import numpy as np
dp = [0]*N
dp[0] = 1
for i in range(N):
    for k in range(K):
        for j in range(l[k], r[k]+1):
            if (i+j < N):
                dp[i+j] = dp[i+j] + dp[i]
            if j == r[k]:
                print(i, (l[k], r[k]), dp, np.diff(dp))
print(dp[-1]%998244353)
'''

# dp : マスiまで移動する方法のパターン数
# dps : dp[i] と dp[i+1] の 差
dp = [0]*N
dps = [0]*(N-1)
dp[0] = 1
dps[0] = -1
for i in range(N-1):
    for k in range(K):
        if i + l[k] - 1 < (N - 1):
            dps[i + l[k] - 1] += dp[i]
        if i + r[k] < (N - 1):
            dps[i + r[k]    ] -= dp[i]
        #print(i, (l[k], r[k]), dp, dps)
    dp[i+1] = (dps[i] + dp[i])%998244353
print((dp[-1]%998244353))
# Original solution
n,k=list(map(int,input().split()))
ans=[0 for _ in range(n*3)]
ans[1]=1

idou=[]

for _ in range(k):
  a=list(map(int,input().split()))
  idou.append(a)
  
    
mod=998244353

rui=[0 for _ in range(n+1)]
rui[1]=1
  
for i in range(2,n+1):
  for g in idou:
    x,y=g
    left=max(0,i-y-1)
    right=max(0,i-x)
    ans[i]+=rui[right]-rui[left]

    
  rui[i]+=((rui[i-1]+ans[i]))

    
    

print((ans[n]%mod))
  

# Optimized solution
n,k=list(map(int,input().split()))
ans=[0 for _ in range(n*3)]
ans[1]=1

idou=[]

for _ in range(k):
  a=list(map(int,input().split()))
  idou.append(a)
  
    
mod=998244353

rui=[0 for _ in range(n+1)]
rui[1]=1
  
for i in range(2,n+1):
  for g in idou:
    x,y=g
    left=max(0,i-y-1)
    right=max(0,i-x)
    ans[i]+=(rui[right]-rui[left])%mod

    
  rui[i]+=((rui[i-1]+ans[i]))%mod

    
    

print((ans[n]%mod))
# Original solution
#editorial
n,k=list(map(int,input().split()))
*lr,=[tuple(map(int, input().split())) for _ in range(k)]
res =[0]*(2*n+3)
##############
diff=[0]*(2*n+3)
######
#res[0]=1

diff[0]=1
diff[1]=-1
mod=998244353

for i in range(n+1):
    res[i]=res[i-1]+diff[i]
    for li,ri in lr:
        diff[i+li]+=res[i]
        diff[i+ri+1]-=res[i]
    #print(res[:n+1])
    #print((diff[:n+1]))
print((res[n-1]%mod))
#print(res[:n+1])
#print((diff[:n+1]))
    

# Optimized solution
#editorial
n,k=list(map(int,input().split()))
*lr,=[tuple(map(int, input().split())) for _ in range(k)]
res =[0]*(2*n+3)
##############
diff=[0]*(2*n+3)
######
#res[0]=1

diff[0]=1
diff[1]=-1
mod=998244353

for i in range(n+1):
    res[i]=res[i-1]+diff[i]
    res[i]%=mod
    diff[i]%=mod
    for li,ri in lr:
        diff[i+li]+=res[i]
        diff[i+ri+1]-=res[i]
    #print(res[:n+1])
    #print((diff[:n+1]))
print((res[n-1]%mod))
#print(res[:n+1])
#print((diff[:n+1]))
# Original solution
n, k = list(map(int, input().split()))

F = [0]*(n+2) # [l,r+1): 1<=[l]<[r+1]<=n+1
for _ in range(k):
    l, r = list(map(int, input().split()))
    F[l] += 1
    F[r+1] += -1

# print(F)
S = []
for i in range(n+1): # i:[0, n]
    if F[i] > 0: S.append(i)
    F[i+1] += F[i]

# print(F)
# print(n,k)
# print(S)

MOD = 998244353
dp = [0]*(n+1)  # [0,n]
dp[1] = 1

for i in range(1, n+1): # i:[1,n]
    for s in S:
        if i + s <= n:
            # print('s {} i {} n {}'.format(s,i,n))
            dp[i + s] += dp[i]
            dp[i + s] %= MOD

# print(dp)

print(((dp[n] + MOD) % MOD))


# Optimized solution
n, k = list(map(int, input().split()))
LR = []
for _ in range(k):
    l, r = list(map(int, input().split()))
    LR.append([l, r+1])

MOD = 998244353
dp = [0]*(n+1)  # [0,n]
dp[0] = 1
dp[1] = -1

for i in range(n): # i:[0,n-1]
    if i > 0: dp[i] += dp[i-1]
    for j in range(k):
        l, r = LR[j]
        if i+l < n:
            dp[i+l] += dp[i]
            dp[i+l] %= MOD
        if i+r < n:
            dp[i+r] -= dp[i]
            dp[i+r] %= MOD

print(((dp[n-1] + MOD) % MOD))