Tak is on Cell 1 and wants to reach Cell N. Given K non-intersecting segments, Tak can move from Cell i to i+d, where d is an integer from the union of these segments. Find the number of ways for Tak to reach Cell N, modulo 998244353. Constraints: 2 ≤ N ≤ 2 × 10^5, 1 ≤ K ≤ min(N, 10), 1 ≤ Li ≤ Ri ≤ N, non-intersecting segments. Input format: N K L1 R1 L2 R2 ... LK RK. Output the number of ways to reach Cell N. Examples: (5 2, 1 1 3 4) → 4, (5 2, 3 3 5 5) → 0, (5 1, 1 2) → 5, (60 3, 5 8 1 3 10 15) → 221823067 (mod 998244353).

# Buggy version:
MOD = 998244353
N, K = map(int, input().split()) #Nはマス目の数、Kは区間の数(Kは10以下)
kukan = [tuple(map(int, input().split())) for _ in range(K)]

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

sumdp = [0] * (N+1)
sumdp[1] = 1

for i in range(2, N+1):
    for l, r in kukan:
        li = max(i - l, 0)
        ri = max(i - r - 1, 0)

        dp[i] += sumdp[li] - sumdp[ri]

    sumdp[i] = sumdp[i-1] + dp[i]

print(dp[N])

# Fixed version:
MOD = 998244353
N, K = map(int, input().split()) #Nはマス目の数、Kは区間の数(Kは10以下)
kukan = [tuple(map(int, input().split())) for _ in range(K)]

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

sumdp = [0] * (N+1)
sumdp[1] = 1

for i in range(2, N+1):
    for l, r in kukan:
        li = max(i - l, 0)
        ri = max(i - r - 1, 0)

        dp[i] += sumdp[li] - sumdp[ri]
        dp[i] %= MOD

    sumdp[i] = sumdp[i-1] + dp[i]

print(dp[N])

# Buggy version:
N, K = map(int, input().split())
MOD = 998244353

l = [0] * K
r = [0] * K

for i in range(K):
	l[i], r[i] = map(int, input().split())
	
dp = [0] * (N+1)
dp[1] = 1

dpsum = [0] * (N+1)
dpsum[1] = 1

for i in range(2, N+1):
	for j in range(K):
		li = i - r[j]
		ri = i - l[j]
		if li < 0:
			continue
		if li == 0:
			dp[i] = dp[i] + dpsum[ri]
		else:
			dp[i] = dpsum[ri] - dpsum[li-1] + dp[i]
			
			
	dpsum[i] = dp[i] + dpsum[i-1]
	
print(dp[N] % MOD)

# Fixed version:
N, K = map(int, input().split())
MOD = 998244353

l = [0] * K
r = [0] * K

for i in range(K):
	l[i], r[i] = map(int, input().split())
	
dp = [0] * (N+1)
dp[1] = 1

dpsum = [0] * (N+1)
dpsum[1] = 1

for i in range(2, N+1):
	for j in range(K):
		li = i - r[j]
		ri = i - l[j]
		if li <= 0:
			dp[i] = dp[i] + dpsum[ri]
			dp[i] %= MOD
		else:
			dp[i] = dpsum[ri] - dpsum[li-1] + dp[i]
			dp[i] %= MOD
			
			
	dpsum[i] = dp[i] + dpsum[i-1]
	dpsum[i] %= MOD
	
print(dp[N] % MOD)

# Buggy version:
#もらうDPで考える
N, K = map(int, input().split())

l = []
r = []

MOD = 998244353

for i in range(K):
    a,b = map(int, input().split())
    l.append(a)
    r.append(b)

dp = [0] * (N+1)
#スタート地点が1なので、dp[1]を1とする
dp[1] = 1

dpsum = [0] * (N+1)
dpsum[1] = 1

for i in range(2, N+1):
    for j in range(K):
        #[l[i], r[i]]
        li = i - r[j]
        ri = i - l[j]
        if ri < 0: continue
        # l1が負になるケースをハンドリング
        li = max(1, li)
        dp[i] += dpsum[ri] - dpsum[li-1] #dp[li] ~ dp[ri]の和を足す
    dpsum[i] = dpsum[i-1] + dp[i]

print(dp[N])

# Fixed version:
#もらうDPで考える
N, K = map(int, input().split())

l = []
r = []

MOD = 998244353

for i in range(K):
    a,b = map(int, input().split())
    l.append(a)
    r.append(b)

dp = [0] * (N+1)
#スタート地点が1なので、dp[1]を1とする
dp[1] = 1

dpsum = [0] * (N+1)
dpsum[1] = 1

for i in range(2, N+1):
    for j in range(K):
        #[l[i], r[i]]
        li = i - r[j]
        ri = i - l[j]
        if ri < 0: continue
        # l1が負になるケースをハンドリング
        li = max(1, li)
        dp[i] += (dpsum[ri] - dpsum[li-1])%MOD #dp[li] ~ dp[ri]の和を足す
    dpsum[i] = (dpsum[i-1] + dp[i])%MOD

print(dp[N]%MOD)

# Buggy version:
N, K = map(int, input().split())

DP = [0]*(N+1)
DP[1] = 1

S = []
for i in range(K):
    S.append(list(map(int, input().split())))

S.sort()

for i in range(2, N+1):
    for l, r in S:
            DP[i] = (DP[max(i-l, 0)] - DP[max(i-r-1, 0)])%998244353
    DP[i] += DP[i-1]

print(DP[N]%998244353)

# Fixed version:
N, K = map(int, input().split())

DP = [0]*(N+1)
DP[1] = 1

S = []
for i in range(K):
    S.append(list(map(int, input().split())))

for i in range(2, N+1):
    for l, r in S:
            DP[i] += (DP[max(i-l, 0)] - DP[max(i-r-1, 0)])
    DP[i] += DP[i-1]
    DP[i] %= 998244353

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

# Buggy version:
n, k = map(int, input().split())
sec = [list(map(int, input().split())) for _ in range(k)]
sec.sort()

mod = 998244353

dp = [0] * n
dp[0] = 1
sum_dp = [0, 1]
for i in range(1, n):
    for l, r in sec:
        if i - l < 0:
            break
        elif i - r > 0:
            dp[i] = ((dp[i] + sum_dp[i-l+1]) % mod - sum_dp[i-r]) % mod
        else:
            dp[i] = (dp[i] + sum_dp[i-l+1]) % mod
    
    sum_dp.append((sum_dp[-1] + dp[i]) % modp)

print(dp[n-1])

# Fixed version:
n, k = map(int, input().split())
sec = [list(map(int, input().split())) for _ in range(k)]
sec.sort()

mod = 998244353

dp = [0] * n
dp[0] = 1
sum_dp = [0, 1]
for i in range(1, n):
    for l, r in sec:
        if i - l < 0:
            break
        elif i - r > 0:
            dp[i] = ((dp[i] + sum_dp[i-l+1]) % mod - sum_dp[i-r]) % mod
        else:
            dp[i] = (dp[i] + sum_dp[i-l+1]) % mod
    
    sum_dp.append((sum_dp[-1] + dp[i]) % mod)

print(dp[n-1])

# Buggy version:
N, K = map(int, input().split())

MOD = 998244353

L_list = []
R_list = []
for i in range(K):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)

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


for i in range(2, N+1):
    for j in range(len(L_list)):
        if i - R_list[j]-1 < 0:
            R_pos = 0
        else:
            R_pos = i - R_list[j] - 1
        dp[i] += (sdp[i - L_list[j]] - sdp[R_pos])%MOD
    sdp[i] = sdp[i-1] + dp[i]
    sdp[i] %= MOD
    #print(dp)
    #print("s", sdp)
print(dp[N])

# Fixed version:
N, K = map(int, input().split())

MOD = 998244353

L_list = []
R_list = []
for i in range(K):
    L, R = map(int, input().split())
    L_list.append(L)
    R_list.append(R)

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


for i in range(2, N+1):
    for j in range(len(L_list)):
        if i - R_list[j]-1 < 0:
            R_pos = 0
        else:
            R_pos = i - R_list[j] - 1
        dp[i] += (sdp[i - L_list[j]] - sdp[R_pos])%MOD
        dp[i] %= MOD
    sdp[i] = sdp[i-1] + dp[i]
    sdp[i] %= MOD
    #print(dp)
    #print("s", sdp)
print(dp[N])

# Buggy version:
n, K = [int(a) for a in input().split()]
s = [[int(a) for a in input().split()] for _ in range(K)]

MOD = 998244353

dp = [0] * (n + 1)
dp_sum = [0] * (n + 1)
dp[1] = 1

for i in range(1, n+1):
    for l, r in s:
         dp[i] += dp_sum[i-l if (i-l) > 0 else 0] - dp_sum[(i-r-1) if (i-r-1) > 0 else 0]
        dp_sum[i] = (dp[i] + dp_sum[i-1]) % MOD

print(dp[n] % MOD)

# Fixed version:
n, K = [int(a) for a in input().split()]
s = [[int(a) for a in input().split()] for _ in range(K)]

MOD = 998244353

dp = [0] * (n + 1)
dp_sum = [0] * (n + 1)
dp[1] = 1

for i in range(1, n+1):
    for l, r in s:
        dp[i] += dp_sum[i-l if (i-l) > 0 else 0] - dp_sum[(i-r-1) if (i-r-1) > 0 else 0]
        dp_sum[i] = (dp[i] + dp_sum[i-1]) % MOD

print(dp[n] % MOD)

# Buggy version:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

mod = 998244353

n, k = map(int, input().split())
l = []
r = []
for _ in range(k):
    l_, r_ = map(int, input().split())
    l.append(l_)
    r.append(r_)
dp = [0] * (n + 1)
dp_csum = [0] * (n + 1)
dp[1] = 1
dp_csum[1] = 1
for i in range(2, n + 1):
    for j in range(k):
        left = i - r[j]
        right = i - l[j]
        if right > 0:
            dp[i] += dp_csum[right] - dp_csum[max(left, 1) - 1]
            dp[i] %= mod
            dp_csum[i] = dp_csum[i - 1] + dp[i]
            dp_csum[i] %= mod
print(dp[-1])

# Fixed version:
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

mod = 998244353

n, k = map(int, input().split())
l = []
r = []
for _ in range(k):
    l_, r_ = map(int, input().split())
    l.append(l_)
    r.append(r_)
dp = [0] * (n + 1)
dp_csum = [0] * (n + 1)
dp[1] = 1
dp_csum[1] = 1
for i in range(2, n + 1):
    for j in range(k):
        left = i - r[j]
        right = i - l[j]
        if right >= 1:
            dp[i] += dp_csum[right] - dp_csum[max(left, 1) - 1]
            dp[i] %= mod
    dp_csum[i] = dp_csum[i - 1] + dp[i]
    dp_csum[i] %= mod
print(dp[-1])

# Buggy version:
n,k=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(k)]

mod = 998244353

dp = [0] * (n+1)
sdp = [0] * (n+1)
dp[1] = 1
sdp[1] = 1

for i in range(n+1):
  for j in range(k):
    left = max(0, i-lr[j][1])
    right = max(0, i-lr[j][0]+1)
    
    dp[i] += sdp[right] - sdp[left]
  if i+1 <= n:
    sdp[i+1] = sdp[i]%mod + dp[i]%mod

print(dp%mod)

# Fixed version:
n,k=map(int,input().split())
lr=[list(map(int,input().split())) for _ in range(k)]

mod = 998244353

dp = [0] * (n+1)
sdp = [0] * (n+1)
dp[1] = 1
sdp[1] = 1

for i in range(n+1):
  for j in range(k):
    left = max(0, i-lr[j][1])
    right = max(0, i-lr[j][0]+1)
    
    dp[i] += sdp[right] - sdp[left]
  if i+1 <= n:
    sdp[i+1] = sdp[i]%mod + dp[i]%mod

print(dp[n]%mod)

# Buggy version:
#dpで素直にやるとO(N^2)だから、累積和で高速化しO(KN)にする

n, k = map(int,input().split())
mod = 998244353

s = []
for i in range(k):
  s.append(list(map(int,input().split())))

cnt = [0]*n
cnt[0] = 1
sum = [0]*n
sum[0] = 1
for i in range(1,n):
  for j in range(k):
    if i > s[j][1]:
      cnt[i] += (sum[i-s[j][0]] - sum[i-s[j][1]-1]) % mod
    elif i >= s[j][0]:
      cnt[i] += sum[i-s[j][0]] % mod
    sum[i] = (sum[i-1] + cnt[i]) % mod

print(cnt[-1])

# Fixed version:
#dpで素直にやるとO(N^2)だから、累積和で高速化しO(KN)にする

n, k = map(int,input().split())
mod = 998244353

s = []
for i in range(k):
  s.append(list(map(int,input().split())))

cnt = [0]*n
cnt[0] = 1
sum = [0]*n
sum[0] = 1
for i in range(1,n):
  for j in range(k):
    if i > s[j][1]:
      cnt[i] += sum[i-s[j][0]] - sum[i-s[j][1]-1]
    elif i >= s[j][0]:
      cnt[i] += sum[i-s[j][0]]
    cnt[i] %= mod
    sum[i] = (sum[i-1] + cnt[i]) % mod

print(cnt[-1])

# Buggy version:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
# from numba import njit,i8


# @njit(i8(i8,i8,i8,i8[:,:],i8[:],i8,i8))
def give_dp(N,K,mod,LR,dp,l,r):
    for i in range(N):
        if i > 0:
            dp[i] += dp[i-1]
            dp[i] %= mod
        for k in range(K):
            l = LR[k][0]
            r = LR[k][1]
            if i + l < N:
                dp[i+l] += dp[i]
                dp[i+1] %= mod
            if i + r < N:
                dp[i+r+1] -= dp[i]
                dp[i+1] %= mod
    return dp[-1]


def main():
    N,K = map(int,input().split())
    LR = [list(map(int,input().split())) for i in range(K)]
    # LR = np.array(LR)

    mod = 998244353

    dp = [0 for i in range(N)]
    dp[0] = 1
    dp[1] = -1
    # dp = np.array(dp)

    res = give_dp(N,K,mod,LR,dp,0,0)
    res %= mod

    print(res)



main()


# Fixed version:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)
# from numba import njit,i8
 
 
# @njit(i8(i8,i8,i8,i8[:,:],i8[:],i8,i8))
def give_dp(N,K,mod,LR,dp,l,r):
    for i in range(N):
        if i > 0:
            dp[i] += dp[i-1]
            dp[i] %= mod
        for k in range(K):
            l = LR[k][0]
            r = LR[k][1]
            if i + l < N:
                dp[i+l] += dp[i]
                dp[i+1] %= mod
            if i + r + 1 < N:
                dp[i+r+1] -= dp[i]
                dp[i+1] %= mod
    return dp[-1]
 
 
def main():
    N,K = map(int,input().split())
    LR = [list(map(int,input().split())) for i in range(K)]
    # LR = np.array(LR)
 
    mod = 998244353
 
    dp = [0 for i in range(N)]
    dp[0] = 1
    dp[1] = -1
    # dp = np.array(dp)
 
    res = give_dp(N,K,mod,LR,dp,0,0)
    res %= mod
 
    print(res)
 
 
 
main()

# Buggy version:
n, k = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(k)]
mod = 998244353

s = []
for l, r in lr:
    s += [i for i in range(l, r + 1)]

dp = [0] * (n + 1)
dp[1] = 1
for i in range(2, n + 1):
    for j in s:
        dp[i] += dp[max(i - j, 0)]
    dp[i] %= mod
print(dp)


# Fixed version:
n, k = map(int, input().split())
lr = [list(map(int, input().split())) for _ in range(k)]
mod = 998244353

dp = [0] * (n + 1)
acc = [0] * (n + 1)
dp[1], acc[1] = 1, 1
for i in range(2, n + 1):
    for l, r in lr:
        dp[i] += acc[max(i - l, 0)]
        dp[i] -= acc[max(i - (r + 1), 0)]
    dp[i] %= mod
    acc[i] = (dp[i] + acc[i - 1]) % mod
print(dp[n])

# Buggy version:
N, K = map(int, input().split())
L=[]
R=[]
for i in range(K):
  l,r= map(int, input().split())
  L.append(l)
  R.append(r)

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

for i in range(N):
  for j in range(K):
    dp[i]+=dpc[max(i-L[j]+1,0)]-dpc[max(i-R[j],0)]
  dpc[i+1]=dpc[i]+dp[i]

print(dp[N-1])

# Fixed version:
N, K = map(int, input().split())
L=[]
R=[]
for i in range(K):
  l,r= map(int, input().split())
  L.append(l)
  R.append(r)

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

for i in range(N):
  for j in range(K):
    dp[i]+=dpc[max(i-L[j]+1,0)]-dpc[max(i-R[j],0)]
  dp[i]%=998244353
  dpc[i+1]=dpc[i]+dp[i]

print(dp[N-1])

# Buggy version:
n,k = (int(x) for x in input().split())
lr = []
for _ in range(k):
    l,r = (int(x) for x in input().split())
    lr.append((l,r))
lr.sort()
print(lr)

mod = 998244353

dp= [0]*(n+1)
dp[1] = 1

for i in range(2,n+1):
    cnt = 0
    for l,r in lr:
        if l >= i:
            break
        else:
            cnt += dp[i-l] - dp[max(0,i-r-1)]
    dp[i] = (dp[i-1] + cnt) % mod

print(cnt%mod)

# Fixed version:
n,k = (int(x) for x in input().split())
lr = []
for _ in range(k):
    l,r = (int(x) for x in input().split())
    lr.append((l,r))
lr.sort()

mod = 998244353

dp= [0]*(n+1)
dp[1] = 1

for i in range(2,n+1):
    cnt = 0
    for l,r in lr:
        if l >= i:
            break
        else:
            cnt += dp[i-l] - dp[max(0,i-r-1)]
    dp[i] = (dp[i-1] + cnt) % mod

print(cnt%mod)

# Buggy version:
N,K = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(K)]
LR.sort()
mod = 998244353
ma = [0] * N
ma[0] = 1
for i in range(N):
    if ma[i] != 0:
        for l,r in LR:
            for j in range(l,r+1):
                if i+j < N:
                    ma[i+j] += ma[i]
                else:
                    f = 1
                    break
            if f:
                break
print(ma[N-1]%mod)

# Fixed version:
N,K = map(int,input().split())
LR = [list(map(int,input().split())) for i in range(K)]
LR.sort()
mod = 998244353
ma = [0] * N
ma[0] = 1
for i in range(N):
    if i >1:
        ma[i] += ma[i-1]
    p = ma[i]%mod
    if p != 0:
        for l,r in LR:
            if i+l <N:
                ma[i+l] += p
            else:
                break
            if i+r+1<N:
                ma[i+r+1] -= p
print(ma[N-1]%mod)

# Buggy version:
import math

CON = 998244353

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

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

def solve():
    dp[1] = 1
    cs[1] = 1

    for i in range(2, N + 1):
        for pts in LR:
            if i - pts[0] > 0: dp[i] += cs[i-pts[0]] 
            if i - pts[1] > 0: dp[i] -= cs[i-pts[1]] 
        dp[i] = dp[i] % CON
        cs[i] = (cs[i-1] + dp[i]) % CON

    print(dp[N])

solve()

# Fixed version:
import math

CON = 998244353

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

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

def solve():
    dp[1] = 1
    cs[1] = 1

    for i in range(2, N + 1):
        for pts in LR:
            if i - pts[0] > 0: dp[i] += cs[i-pts[0]] 
            if i - pts[1] > 0: dp[i] -= cs[i-(pts[1] + 1)] 
        dp[i] = dp[i] % CON
        cs[i] = (cs[i-1] + dp[i]) % CON

    print(dp[N])

solve()

# Buggy version:
n, k = map(int, input().split())
cum = [0, 1]
dp = [0, 1]

lr = [list(map(int, input().split())) for _ in range(k)]    
mod = 998244353
# i: 目標のマス
def check(i):
    res = 0
    for l, r in lr:
        if i - l < 1:
            break
        res += cum[i-l] - cum[max(i-r-1,0)]
        res %= mod
    return res


for i in range(2,n+1):
    dp.append(check(i))
    cum.append((cum[-1]+dp[-1])%mod)

print(dp[-1])

# Fixed version:
n, k = map(int, input().split())
cum = [0, 1]
dp = [0, 1]

lr = [list(map(int, input().split())) for _ in range(k)]    
mod = 998244353
# i: 目標のマス
def check(i):
    res = 0
    for l, r in lr:
        if i - l < 1:
            continue
        res += cum[i-l] - cum[max(i-r-1,0)]
        res %= mod
    return res


for i in range(2,n+1):
    dp.append(check(i))
    cum.append((cum[-1]+dp[-1])%mod)

print(dp[-1])

# Buggy version:
MOD = 998244353

N, K = map(int, input().split())
seg = [tuple(map(int, input().split())) for _ in range(K)]
dp = [0] * (N+1)
dp[0] = 1
for i in range(N):
    for s, t in seg:
        dp[i+s] += dp[i]
        dp[i+t;1] -= dp[i]
    dp[i+1] += dp[i]
    dp[i+1] %= MOD
    
print(dp[N-1])


# Fixed version:
MOD = 998244353

N, K = map(int, input().split())
seg = [tuple(map(int, input().split())) for _ in range(K)]
dp = [0] * (N+1)
dp[0] = 1
for i in range(N):
    for s, t in seg:
        if (i+s) <= N:dp[i+s] += dp[i]
        if (i+t+1) <= N:dp[i+t+1] -= dp[i]
    dp[0] = 0
    dp[i+1] += dp[i]
    dp[i+1] %= MOD
    
print(dp[N-1])

# Buggy version:
def d_leaping_tak(MOD=998244353):
    from bisect import bisect_left, bisect_right
    N, K = [int(i) for i in input().split()]
    Ranges = sorted([[int(i) for i in input().split()] for j in range(K)])

    dp = [0] * (N + 1)  # dp[i]: マス i への行き方
    dp[1] = 1
    cumsum_dp = dp[:]
    for i in range(2, N + 1):
        for l, r in Ranges:
            if i - l < 0:
                continue
            if i - r < 0:
                r = i

            dp[i] += cumsum_dp[i - l] - cumsum_dp[i - r - 1]
        dp[i] %= MOD
        cumsum_dp[i] = cumsum_dp[i - 1] + dp[i]

print(d_leaping_tak())

# Fixed version:
def d_leaping_tak(MOD=998244353):
    from bisect import bisect_left, bisect_right
    N, K = [int(i) for i in input().split()]
    Ranges = sorted([[int(i) for i in input().split()] for j in range(K)])

    dp = [0] * (N + 1)  # dp[i]: マス i への行き方
    dp[1] = 1
    cumsum_dp = dp[:]
    for i in range(2, N + 1):
        for l, r in Ranges:
            if i - l < 0:
                continue
            if i - r < 0:
                r = i

            dp[i] += cumsum_dp[i - l] - cumsum_dp[i - r - 1]
        dp[i] %= MOD
        cumsum_dp[i] = cumsum_dp[i - 1] + dp[i]
    # print(dp)
    # print(cumsum_dp)
    return dp[N]

print(d_leaping_tak())

# Buggy version:
N, K = map(int, input().split())
S = []
for j in range(K):
    S.append(list(map(int, input().split())))

m =  998244353

print(S)

dp = [0] * N
dp[0] = 1
sum_of_region = [0] * K
for i in range(1, N):
    for j in range(K):
        if i - S[j][0] >= 0:
            sum_of_region[j] += dp[i - S[j][0]]
        if i - S[j][1] - 1 >= 0:
            sum_of_region[j] -= dp[i - S[j][1] - 1]
        dp[i] += sum_of_region[j] % m
        dp[i] %= m
    print(i, dp, sum_of_region)

ans = dp[N - 1]
print(ans)


# Fixed version:
N, K = map(int, input().split())
S = []
for j in range(K):
    S.append(list(map(int, input().split())))

m =  998244353

# print(S)

dp = [0] * N
dp[0] = 1
sum_of_region = [0] * K
for i in range(1, N):
    for j in range(K):
        if i - S[j][0] >= 0:
            sum_of_region[j] += dp[i - S[j][0]]
        if i - S[j][1] - 1 >= 0:
            sum_of_region[j] -= dp[i - S[j][1] - 1]
        dp[i] += sum_of_region[j] % m
        dp[i] %= m
    # print(i, dp, sum_of_region)

ans = dp[N - 1]
print(ans)

# Buggy version:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
MOD = 998244353

n, k = map(int, input().split())
dp = [0] * n
dp[0] = 1
dp[1] = -1 % MOD
delta = []
for _ in range(k):
    l, r = map(int, input().split())
    delta.append((l, 1))
    delta.append((r+1, -1 % MOD))

total = 0
for i in range(n):
    total += dp[i]
    total %= MOD
    for p, d in delta:
        if i + p >= n:
            break
        dp[i + p] += total * d
        dp[i + p] %= MOD
print(total % MOD)

# Fixed version:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
MOD = 998244353

n, k = map(int, input().split())
dp = [0] * n
dp[0] = 1
dp[1] = -1
delta = []
for _ in range(k):
    l, r = map(int, input().split())
    delta.append((l, 1))
    delta.append((r+1, -1))

total = 0
for i in range(n):
    total += dp[i]
    total %= MOD
    for p, d in delta:
        if i + p >= n:
            continue
        dp[i + p] += total * d
        dp[i + p] %= MOD
print(total % MOD)

# Buggy version:
mod = 998244353
n,k = 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):
    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)] = (dp[i]+dp2[min(i+l,n)])%mod
        dp2[min(i+r+1,n)] = (dp[i]+dp2[min(i+r+1,n)])%mod
print(dp[n-1]%mod)


# Fixed version:
mod = 998244353
n,k = 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)

# Buggy version:
n,k = map(int,input().split())
lr = []
for _ in range(k):
  lr.append(list(map(int,input().split())))
print(lr)
imos = [0 for _ in range(n)]
imos[0] = 1
imos[1] = -1
now = 0
mod = 998244353
for i in range(n):
    now += imos[i]
    now %= mod
    #print(now)
    for j in range(k):
        try:
            imos[i+lr[j][0]] += now
            imos[i+lr[j][0]] %= mod
        except:
            pass
        try:
            imos[i+lr[j][1]+1] -= now
            imos[i+lr[j][1]+1] %= mod
        except:
            pass
    #print(imos)
print(now)

# Fixed version:
n,k = map(int,input().split())
lr = []
for _ in range(k):
  lr.append(list(map(int,input().split())))
#print(lr)
imos = [0 for _ in range(n)]
imos[0] = 1
imos[1] = -1
now = 0
mod = 998244353
for i in range(n):
    now += imos[i]
    now %= mod
    #print(now)
    for j in range(k):
        try:
            imos[i+lr[j][0]] += now
            imos[i+lr[j][0]] %= mod
        except:
            pass
        try:
            imos[i+lr[j][1]+1] -= now
            imos[i+lr[j][1]+1] %= mod
        except:
            pass
    #print(imos)
print(now)