CodeEff/ECCO huggingface dataset:
# Original solution
import math

class SegmentTree():
    def __init__(self, size):
        self.size = 2**((size-1).bit_length())
        self.val = [0]*(self.size*2)
    
    def update(self, i, x):
        i += self.size
        self.val[i] = x
        
        while i > 0:
            i = i >> 1
            self.val[i] = self.val[2*i] + self.val[2*i+1]
    
    def rangeSum(self, l, r):
        l += self.size
        r += self.size

        l_sum = 0
        r_sum = 0

        while l < r:
            if l & 1:
                l_sum += self.val[l]
                l += 1
            l = l >> 1

            if r & 1:
                r -= 1
                r_sum += self.val[r]
            r = r >> 1

        return l_sum + r_sum
        
p = 998244353

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

hop = []

for i in range(K):
    L, R = list(map(int, input().split()))
    for j in range(L, R + 1):
        hop.append(j)

dp = SegmentTree(N)

dp.update(0,1)

for i in range(N - 1):
    for j in range(len(hop)):
        d_val = dp.rangeSum(i, i + 1) % p
        if i + hop[j] < N:
            d_val_n = dp.rangeSum(i + hop[j], i + hop[j] + 1) % p
            dp.update(i + hop[j], d_val_n + d_val)

print((dp.rangeSum(N - 1, N) % p))

# Optimized solution
class BITRAQ:
    def __init__(self, n):
        self.n = n + 1
        self.bit = [[0]*(self.n) for _ in range(2)]
    
    def add_sub(self, p, i, x):
        while i < self.n:
            self.bit[p][i] += x
            i += i & -i
    
    def add(self, l, r, x):
        self.add_sub(0, l, -x * (l - 1))
        self.add_sub(0, r, x * (r - 1))
        self.add_sub(1, l, x)
        self.add_sub(1, r, -x)

    def sum_sub(self, p, i):
        s = 0
        while i > 0:
            s += self.bit[p][i]
            i -= i & -i
        return s
    
    def sum(self, i):
        return self.sum_sub(0, i) + self.sum_sub(1, i) * i

    def get(self, i):
        return self.sum(i) - self.sum(i - 1)

p = 998244353

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

hop = []

for i in range(K):
    hop.append(tuple(map(int, input().split())))

dp = BITRAQ(N)

dp.add(1, 2, 1)

for i in range(N - 1):
    for j in range(K):
        dp.add(i + hop[j][0] + 1, min(N + 1, i + hop[j][1] + 2), dp.get(i + 1) % p)

print((dp.get(N) % p))
# Original solution
class BITRAQ:
    def __init__(self, n):
        self.n = n + 1
        self.bit = [[0]*(self.n) for _ in range(2)]
    
    def add_sub(self, p, i, x):
        while i < self.n:
            self.bit[p][i] += x
            i += i & -i
    
    def add(self, l, r, x):
        self.add_sub(0, l, -x * (l - 1))
        self.add_sub(0, r, x * (r - 1))
        self.add_sub(1, l, x)
        self.add_sub(1, r, -x)

    def sum_sub(self, p, i):
        s = 0
        while i > 0:
            s += self.bit[p][i]
            i -= i & -i
        return s
    
    def sum(self, i):
        return self.sum_sub(0, i) + self.sum_sub(1, i) * i

    def get(self, i):
        return self.sum(i) - self.sum(i - 1)

p = 998244353

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

hop = []

for i in range(K):
    hop.append(tuple(map(int, input().split())))

dp = BITRAQ(N)

dp.add(1, 2, 1)

for i in range(N - 1):
    for j in range(K):
        dp.add(i + hop[j][0] + 1, min(N + 1, i + hop[j][1] + 2), dp.get(i + 1) % p)

print((dp.get(N) % p))

# Optimized solution
class BITRAQ:
    def __init__(self, n, mod):
        self.n = n + 1
        self.mod = mod
        self.bit = [[0]*(self.n) for _ in range(2)]
    
    def add_sub(self, p, i, x):
        while i < self.n:
            self.bit[p][i] += x
            self.bit[p][i] %= self.mod
            i += i & -i
    
    def add(self, l, r, x):
        self.add_sub(0, l, -x * (l - 1))
        self.add_sub(0, r, x * (r - 1))
        self.add_sub(1, l, x)
        self.add_sub(1, r, -x)

    def sum_sub(self, p, i):
        s = 0
        while i > 0:
            s += self.bit[p][i]
            i -= i & -i
        return s
    
    def sum(self, i):
        return self.sum_sub(0, i) + self.sum_sub(1, i) * i

    def get(self, i):
        return self.sum(i) % self.mod - self.sum(i - 1) % self.mod

p = 998244353

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

hop = []

for i in range(K):
    hop.append(tuple(map(int, input().split())))

dp = BITRAQ(N, p)

dp.add(1, 2, 1)

for i in range(N - 1):
    for j in range(K):
        dp.add(i + hop[j][0] + 1, min(N + 1, i + hop[j][1] + 2), dp.get(i + 1) % p)

print((dp.get(N) % p))
# Original solution
mod = 998244353

def main(N, S):
    dp = [0 if n != 0 else 1 for n in range(N)] # 初期値
    lst_S = sorted(list(S))

    for i in range(N):
        if dp[i] == 0:  # 計算時間削減のため， 時間さえ間に合えば不要かも．
            continue
        for j in lst_S:
            if i + j > N - 1:
                break
            else:
                dp[i+j] = (dp[i+j] + dp[i]) % mod
    print((dp[-1]))


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    S = set()
    for k in range(K):
        L, R = list(map(int, input().split()))
        {S.add(i) for i in range(L, R+1)}
    main(N, S)

# Optimized solution
mod = 998244353

# 貰うDP
def main(N, S):
    dp = [0 if n != 0 else 1 for n in range(N)]     # dp[i]はマスiに行く通り数． (答えはdp[-1]), dp[0] = 1 (最初にいる場所だから1通り)
    A = [0 if n != 0 else 1 for n in range(N)]      # dp[i] = A[i] - A[i-1] (i >= 1), A[0] = dp[0] = 1 (i = 0) が成り立つような配列を考える．

    for i in range(1, N):   # 今いる点 (注目点)
        for l, r in S:  # 選択行動範囲 (l: 始点, r: 終点)
            if i - l < 0:   # 注目点が選択行動範囲の始点より手前の場合 → 注目点に来るために使用することはできない．
                break
            else:           # 注目点に来るために使用することができる場合
                dp[i] += A[i-l] - A[max(i-r, 0)-1]  # lからrの間で，注目点に行くために使用できる点を逆算． そこに行くことができる = 選択行動範囲の値を選択することで注目点に達することができる通り数．
        dp[i] %= mod
        A[i] = (A[i-1] + dp[i]) % mod
    print((dp[-1]))

if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    S = {tuple(map(int, input().split())) for k in range(K)}
    S = sorted(list(S), key = lambda x:x[0])    # 始点でsort (範囲の重複がないため)
    main(N, S)
# Original solution
import sys
sys.setrecursionlimit(10**9)

N, K = list(map(int, input().split()))
S = set()

MOD = 998244353
for _ in range(K):
    L, R = list(map(int, input().split()))
    S |= set(range(L, R + 1))
# print(f'{S=}')

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


def f(idx):
    ans = 0
    for s in S:
        if idx - s < 0:
            continue
        if DP[idx - s] == -1:
            DP[idx - s] = f(idx - s)
        ans += DP[idx - s]
    return ans % MOD


ans = f(N)
# print(f'{DP=}')
print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
MOD = 998244353
LR = []
for _ in range(K):
    L, R = list(map(int, input().split()))
    LR.append((L, R))
# print(f'{LR=}')

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

for i in range(1, len(DP)):
    for L, R in LR:
        v = S[max(i - L, 0)] - S[max(i - R - 1, 0)]
        DP[i] += v
    DP[i] %= MOD
    S[i] = (S[i - 1] + DP[i]) % MOD

# print(f'{DP=}')
# print(f'{S=}')
print((DP[-1]))
# Original solution
from itertools import *
n, k = list(map(int, input().split()))
mod = 998244353
strides = []
for i in range(k):
  l, r = list(map(int, input().split()))
  for t in range(l, r + 1):
    strides.append(t)
strides.sort()

d = [0] * (n + 1)  # マスに対応
d[0] = 1
for i in range(1, n):
  for stride in strides:
    if i >= stride:
      d[i] += d[i - stride]
  d[i] %= mod
print((d[n-1]))

# Optimized solution
n, k = list(map(int, input().split()))
mod = 998244353
d = [0] * (n + 1)
d[1] = 1
LR =[list(map(int, input().split())) for _ in range(k)]

for i in range(2, n+1):
  d[i] = d[i - 1]
  for l, r in LR:
    if i - l <= 0:
      continue
    d[i] = (d[i] + (d[i-l] - d[max(0,i-r-1)])) % mod
print(((d[-1] - d[-2])%mod))
# Original solution
import heapq
n,k=list(map(int,input().split()))
can=[]
for i in range(k):
    l,r=list(map(int,input().split()))
    for j in range(l,r+1):
        can.append(j)
table=[0]*n
table[0]=1
ka=[0]*(n+1)
ka[0]=1
que=[0]
heapq.heapify(que)
can.sort()
while que:
    now=heapq.heappop(que)
    ka[now]=0
    for j in can:
        if now+j>=n:
            break
        table[now+j]=(table[now+j]+table[now])%998244353
        if ka[now+j]==0:
            heapq.heappush(que,now+j)
            ka[now+j]=1
print((table[-1]%998244353))

# Optimized solution
p=998244353
n,k=list(map(int,input().split()))
kukan=[]
for i in range(k):
    l,r=list(map(int,input().split()))
    kukan.append((l,r))
dp=[0]*n
sdp=[0]*(n+1)
dp[0]=1
sdp[1]=1
for i in range(1,n):
    for l,r in kukan:
        left=max(0,i-r)
        right=max(0,i-l+1)
        dp[i]=(dp[i]+sdp[right]-sdp[left])%p
    sdp[i+1]=(sdp[i]+dp[i])%p
print((dp[-1]))
# Original solution
def resolve():
    #n=int(input())
    #a,b=map(int,input().split())
    #x=list(map(int,input().split()))
    #a=[list(map(lambda x:int(x)%2,input().split())) for _ in range(h)]
    n,k=list(map(int,input().split()))
    a=set()
    MOD=998244353
    for i in range(k):
      l,r=list(map(int,input().split()))
      for j in range(l-1,r):
        a.add(j)
    dp=[0]*n*2
    dp[0]=1
    for i in range(1,n):
      x=dp[i-1]
      if x==0:
        continue
      else:
        for j in a:
          dp[i+j]+=x
          dp[i+j]%=MOD
    print((dp[n-1]))
    
if __name__ == '__main__':
    resolve()

# Optimized solution
def resolve():
    #n=int(input())
    #a,b=map(int,input().split())
    #x=list(map(int,input().split()))
    #a=[list(map(lambda x:int(x)%2,input().split())) for _ in range(h)]
    n,k=list(map(int,input().split()))
    MOD=998244353
    lr=[list(map(int,input().split())) for _ in range(k)]
    dp=[0]*(n+1)
    dpsum=[0]*(n+1)
    dp[1]=1
    dpsum[1]=1
    for i in range(2,n+1):
      for l,r in lr:
        li=i-r
        ri=i-l
        if r<0:continue
        li=max(1,li)
        dp[i]+=dpsum[ri]-dpsum[li-1]
      dpsum[i]=(dpsum[i-1]+dp[i])%MOD
    print((dp[n]%MOD))
    
if __name__ == '__main__':
    resolve()
# Original solution
MOD = 998244353
class ModInt(int):
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other)

    def __sub__(self, other):
        return ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other)

    def __mul__(self, other):
        return ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other)

    def __floordiv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, MOD - 2, MOD))
        else:
            if other == 0:
                raise ZeroDivisionError
            else:
                return ModInt(self.x * pow(other, MOD - 2, MOD))

    def __pow__(self, other):
        return ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else ModInt(pow(self.x, other, MOD))

    __radd__ = __add__

    def __rsub__(self, other):
        return ModInt(other.x - self.x) if isinstance(other, ModInt) else ModInt(other - self.x)

    __rmul__ = __mul__

    def __rfloordiv__(self, other):
        return ModInt(other.x * pow(self.x, MOD - 2, MOD)) if isinstance(other, ModInt) else ModInt(other * pow(self.x, MOD - 2, MOD))

    def __rpow__(self, other):
        return ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else ModInt(pow(other, self.x, MOD))


def main():
    n, k = list(map(int, input().split()))
    L, R = [], []
    for _ in range(k):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)
    dp = [0] * (n + 1)
    s = [0] * (n + 1)
    dp[1] = ModInt(1)
    s[1] = ModInt(1)
    for i in range(2, n + 1):
        for j in range(k):
            l = max(1, i - R[j])
            r = i - L[j]
            dp[i] += s[r] - s[l - 1]
        s[i] = s[i - 1] + dp[i]
    print((dp[n]))


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    MOD = 998244353
    n, k = list(map(int, input().split()))
    L, R = [], []
    for _ in range(k):
        l, r = list(map(int, input().split()))
        L.append(l)
        R.append(r)
    dp = [0] * (n + 1)
    s = [0] * (n + 1)
    dp[1] = 1
    s[1] = 1
    for i in range(2, n + 1):
        for j in range(k):
            l = max(1, i - R[j])
            r = i - L[j]
            if r < 1: continue
            dp[i] += s[r] - s[l - 1]
            dp[i] %= MOD
        s[i] = s[i - 1] + dp[i]
        s[i] %= MOD
    print((dp[n]))


if __name__ == "__main__":
    main()
# Original solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  n,k=LI()
  field=[LI() for _ in range(k)]
  field.sort()

  rng=[]
  for x,y in field:
    for z in range(x,y+1):
      rng.append(z)

  # print(rng)

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

  rng_mx=len(rng)
  for i in range(1,n):
    for j,dx in enumerate(rng[:rng_mx]):
      if i+dx>n:
        rng_mx=j+1
        break
      dp[i+dx]+=dp[i]
      dp[i+dx]%=mod
    # print(dp)
  
  return dp[n]%mod

# main()
print((main()))


# Optimized solution
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=998244353
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())

def main():
  n,k=LI()
  field=[LI() for _ in range(k)]

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

  for i in range(2,n+1):
    for l,r in field:
      li=i-r
      ri=i-l
      if ri<=0:
        continue
      li=max(1,li)
      dp[i]+=dpsum[ri]-dpsum[li-1]
      dp[i]%=mod
    dpsum[i]=dp[i]+dpsum[i-1]
    dpsum[i]%=mod
  
  return dp[n]%mod

# main()
print((main()))
# Original solution
inf=998244353


N,K=list(map(int,input().split()))
SS=[]

for i in range(K):
    s1,s2=list(map(int,input().split()))
    for j in range(s1,s2+1):
        SS.append(j)

SS.sort()

table = [0 for k in range(N-1)] 

for s in SS:
    if s<=N-1:
        table[s-1]+=1
for i in range(N-1):
    if table[i]!=0:
        table[i]%=inf
        for s in SS:
            if i+s<N-1:
                table[i+s]+=table[i]
                #print(table,i,s)
            else:
                break
print((table[-1]%inf))


# Optimized solution
inf=998244353


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

SS.sort()

table = [0 for k in range(N)] 
diff = [0 for k in range(N-1)]

table[0]=1
diff[0]=-1

for i in range(N-1):
    if table[i]!=0:
        for s in SS:
            lef=s[0]
            rig=s[1]
            if i+lef-1<N-1:
                diff[i+lef-1]+=table[i]
            if i+rig<N-1:
                diff[i+rig]-=table[i]
    table[i+1]=table[i]+diff[i]
    table[i+1]%=inf

print((table[N-1]))
# Original solution
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, LR):
    mod = 998244353
    S = []
    for lr in LR:
        S += [i for i in range(lr[0], lr[1] + 1)]
    S.sort()
    dp = [0] * (N + 1)
    dp[1] = 1
    for i in range(1, N + 1):
        for s in S:
            if i + s > N:
                break
            dp[i + s] = (dp[i + s] + dp[i]) % mod
    print((dp[N]))


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    LR = [[int(i) for i in input().split()] for _ in range(K)]
    solve(N,K,LR)

    # # test
    # from random import randint
    # from func import random_str
    # N, K = 2 * 10 ** 5, 1
    # LR = [[1, N]]
    # solve(N, K, LR)


# Optimized solution
# 解説などを参考に作成
"""
N = 10, LR = [2, 4] を例とする.
fi:=マスiまで移動する方法の個数とおく.
便宜上0マスから始めると、
    N:  0 1 2 3 4 5 6 7 8 9 10
    fi: 0 1 0 1 1 2 2 4 5 8 11
となる.(1マス目には最初からいるのでf1 = 1とする.)
ここで、N=5,6の時で考えると、LR=[2,4]から
    f5 = f1 + f2 + f3  (∵2～4マス前からしか遷移がないので)
    f6 =      f2 + f3 + f4
よってf5とf6の差はf1とf4によって求めることができる.
    f6 = f5 - f1 + f4
一般化すると、
    fi = fi-1 - fi-1-R + fi-L
LRが複数ある場合でも区間が重複しないので、各区間に対して - fi-1-R + fi-L 部分を計算して fi を求めることができる.
計算量はO(KN) (K:=重複しない区間LRの個数)
"""


# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, LR):
    mod = 998244353
    dp = [0] * (N + 1)
    dp[1] = 1
    # dp[0] は基本0として扱ってよいが, dp[2]の計算時のみ,dp[0]とdp[1]の差分=0となる必要がある.
    # => この時のみ、dp[0]を1として計算しないと fi = fi-1 - fi-1-R + fi-L のルールに合わなくなる.
    # 他の人の解答(#16897281, #16895566)見ると階差数列？敵なのを持つことで解決できそうだったけど,
    # 内容が理解できなかったのでdp[2]までは手動実装することにした.
    dp[2] = 1 if 1 in [lr[0] for lr in LR] else 0
    for i in range(3, N + 1):
        x = 0
        for l, r in LR:
            x += dp[max(i - l, 0)] - dp[max(i - 1 - r, 0)]
        dp[i] = (dp[i - 1] + x) % mod
    print((dp[N]))


if __name__ == '__main__':
    N, K = list(map(int, input().split()))
    LR = [[int(i) for i in input().split()] for _ in range(K)]
    solve(N, K, LR)

    # # test
    # from random import randint
    # from func import random_str
    # N, K = 2 * 10 ** 5, 1
    # LR = [[1, N]]
    # solve(N, K, LR)
# Original solution
N, K = list(map(int, input().split()))

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

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

for i in range(2, N+1):
    for s in S:
        l = i - s[1]
        r = i - s[0]
        if r < 1:continue
        l = max(1, l)

        dp[i] += dpsum[r] - dpsum[l-1]

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

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


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

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

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

for i in range(2, N+1):
    for s in S:
        l = i - s[1]
        r = i - s[0]
        if r < 1:continue
        l = max(1, l)

        dp[i] += dpsum[r] - dpsum[l-1]
        dp[i] %= mod

    dpsum[i] = (dpsum[i-1] + dp[i]) % mod

print((dp[-1]))
# Original solution
n, k = list(map(int, input().split()))

s = set()
for i in range(k):
    l, r = list(map(int, input().split()))
    for i in range(l, r+1):
        s.add(i)

mod = 998244353
col = list(s)
counts = [0]*(n+1)
for i in range(1, n+1):
    for c in col:
        if c > i:
            break
        if c == i:
            counts[i] += 1
            counts[i] = counts[i] % mod
            continue
        else:
            counts[i] += counts[i-c]
            counts[i] = counts[i] % mod

print((counts[n-1] % mod))

# Optimized solution
n, k = list(map(int, input().split()))
# n, k = (2*10**5, 10)


ranges = []
for i in range(k):
    l, r = list(map(int, input().split()))
    # f = (2 * 10 ** 5) // k
    # l, r = (f * i, f*(i+1))
    ranges.append((l, r))

mod = 998244353
counts = [0]*(n+1)
sum_dp = [0]*(n+2)
counts[0] = 1
sum_dp[1] = 1
for i in range(1, n+1):
    for r in ranges:
        left = max(0, i - r[1])
        right = max(0, i - r[0] + 1)
        counts[i] += (sum_dp[right] - sum_dp[left])
        counts[i] = counts[i] % mod
    sum_dp[i+1] = sum_dp[i] + counts[i]


print((counts[n-1] % mod))
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 998244353


class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0] * (n + 1)
        self.step = pow(2, n.bit_length() - 1)

    def add(self, i, x=1):
        i += 1
        while i <= self.n:
            self.data[i] += x
            i += i & -i

    def get_sum(self, i):
        i += 1
        x = 0
        while i > 0:
            x += self.data[i]
            i -= i & -i
        return x

    # Return sum for [l, r)
    def get_sum_range(self, l, r):
        return self.get_sum(r - 1) - self.get_sum(l - 1)


def main():
    N, K, *LR = list(map(int, read().split()))

    interval = [(l, r) for l, r in zip(*[iter(LR)] * 2)]

    bit = BIT(N + 2)
    bit.add(1, 1)
    bit.add(2, -1)

    for i in range(N):
        cur = bit.get_sum(i) % MOD
        for l, r in interval:
            bit.add(i + l, cur)
            bit.add(i + r + 1, -cur)

    print((bit.get_sum(N) % MOD))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 998244353


def main():
    N, K, *LR = list(map(int, read().split()))
    interval = [(l, r) for l, r in zip(*[iter(LR)] * 2)]

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

    for i in range(1, N + 1):
        dp[i] = (dp[i] + dp[i - 1]) % MOD
        cur = dp[i]
        for l, r in interval:
            if i + l <= N:
                dp[i + l] += cur
                dp[min(i + r + 1, N + 1)] -= cur

    print((dp[N]))
    return


if __name__ == '__main__':
    main()
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 998244353


def main():
    N, K, *LR = list(map(int, read().split()))
    interval = [(l, r) for l, r in zip(*[iter(LR)] * 2)]

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

    for i in range(1, N + 1):
        dp[i] = (dp[i] + dp[i - 1]) % MOD
        cur = dp[i]
        for l, r in interval:
            if i + l <= N:
                dp[i + l] += cur
                dp[min(i + r + 1, N + 1)] -= cur

    print((dp[N]))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 998244353


def main():
    N, K, *LR = list(map(int, read().split()))
    interval = [(l, r) for l, r in zip(*[iter(LR)] * 2)]

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

    for i in range(1, N + 1):
        dp[i] = (dp[i] + dp[i - 1]) % MOD
        for l, r in interval:
            if i + l <= N:
                dp[i + l] += dp[i]
                if i + r + 1 <= N:
                    dp[i + r + 1] -= dp[i]

    print((dp[N]))
    return


if __name__ == '__main__':
    main()
# Original solution
n,k=list(map(int,input().split()))
B=[[0]*2 for i in range(k)]
for i in range(k):
  B[i]=list(map(int,input().split()))
mod=998244353
A=[0]*(n+1)
A[0]=1
a=1
for i in range(n):
  for j in range(k):
    A[min(n,i+B[j][0])]=A[min(n,i+B[j][0])]+a
    A[min(n,i+B[j][1]+1)]=A[min(n,i+B[j][1]+1)]-a
  a=a+A[i+1]
print((A[-2]%mod))

# Optimized solution
n,k=list(map(int,input().split()))
B=[[0]*2 for i in range(k)]
for i in range(k):
  B[i]=list(map(int,input().split()))
mod=998244353
A=[0]*(n+1)
A[0]=1
a=1
for i in range(n):
  for j in range(k):
    A[min(n,i+B[j][0])]=(A[min(n,i+B[j][0])]+a)%mod
    A[min(n,i+B[j][1]+1)]=(A[min(n,i+B[j][1]+1)]-a)%mod
  a=a+A[i+1]
print((A[-2]%mod))
# Original solution
N,K=list(map(int,input().split()))
dp=[0]*(N)
dp[0]=1
mod=998244353
L=list()
for i in range(K):
  a,b=list(map(int,input().split()))
  L.append([a,b])
for i in range(N):
  for j in range(K):
    a,b=L[j]
    if i>=b:
      dp[i]+=sum(dp[i-b:i-a+1])
    elif i>=a:
      dp[i]+=sum(dp[:i-a+1])
    dp[i]%=mod
print((dp[-1]))

# Optimized solution
N,K=list(map(int,input().split()))
dp=[0]*(N)
dp[0]=1
mod=998244353
L=list()
for i in range(K):
  a,b=list(map(int,input().split()))
  L.append([a,b])
R=[0,1]
for i in range(1,N):
  for j in range(K):
    a,b=L[j]
    if i>=b:
      dp[i]+=R[i-a+1]-R[i-b]
    elif i>=a:
      dp[i]+=R[i-a+1]
    dp[i]%=mod
  R.append(R[-1]+dp[i])
print((dp[-1]))