import sys

input = sys.stdin.readline



class FenwickTree:

    def __init__(self, size):

        self.bit = [0] * (size + 1)

        self.size = size



    def sum(self, i):

        i += 1

        s = 0

        while i > 0:

            s += self.bit[i]

            s %= mod

            i -= i & -i

        return s



    def add(self, i, x):

        i += 1

        while i <= self.size:

            self.bit[i] += x

            self.bit[i] %= mod

            i += i & -i



mod = 998244353

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

L, R = list(zip(*[list(map(int, input().split())) for _ in range(K)]))

dp = FenwickTree(N + 1)

dp.add(1, 1)

dp.add(2, -1)

for i in range(1, N):

    x = dp.sum(i)

    for j in range(K):

        dp.add(i + L[j], x)

        if i + R[j] + 1 <= N:

            dp.add(i + R[j] + 1, -x)

print((dp.sum(N)))
import sys

input = sys.stdin.readline



mod = 998244353

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

L, R = list(zip(*[list(map(int, input().split())) for _ in range(K)]))

dp = [0] * (N + 1)

dp[1] = 1

dp[2] = -1

x = 0

for i in range(1, N):

    x = (x + dp[i]) % mod

    for j in range(K):

        if i + L[j] > N:

            continue

        dp[i + L[j]] = (dp[i + L[j]] + x) % mod

        if i + R[j] + 1 <= N:

            dp[i + R[j] + 1] = (dp[i + R[j] + 1] - x) % mod

x = (x + dp[N]) % mod

print(x)
import sys

input = sys.stdin.readline


class FenwickTree:
    def __init__(self, size):
        self.bit = [0] * (size + 1)
        self.size = size

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

    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.bit[i] += x
            self.bit[i] %= mod
            i += i & -i


mod = 998244353
N, K = list(map(int, input().split()))
L, R = list(zip(*[list(map(int, input().split())) for _ in range(K)]))
dp = FenwickTree(N + 1)
dp.add(1, 1)
dp.add(2, -1)
for i in range(1, N):
    x = dp.sum(i)
    for j in range(K):
        dp.add(i + L[j], x)
        if i + R[j] + 1 <= N:
            dp.add(i + R[j] + 1, -x)
print((dp.sum(N)))

import sys

input = sys.stdin.readline
mod = 998244353
N, K = list(map(int, input().split()))
L, R = list(zip(*[list(map(int, input().split())) for _ in range(K)]))
dp = [0] * (N + 1)
dp[1] = 1
dp[2] = -1
x = 0
for i in range(1, N):
    x = (x + dp[i]) % mod
    for j in range(K):
        if i + L[j] > N:
            continue
        dp[i + L[j]] = (dp[i + L[j]] + x) % mod
        if i + R[j] + 1 <= N:
            dp[i + R[j] + 1] = (dp[i + R[j] + 1] - x) % mod
x = (x + dp[N]) % mod
print(x)

- - -class FenwickTree: -    def __init__(self, size): -        self.bit = [0] * (size + 1) -        self.size = size - -    def sum(self, i): -        i += 1 -        s = 0 -        while i > 0: -            s += self.bit[i] -            s %= mod -            i -= i & -i -        return s - -    def add(self, i, x): -        i += 1 -        while i <= self.size: -            self.bit[i] += x -            self.bit[i] %= mod -            i += i & -i - - -dp = FenwickTree(N + 1) -dp.add(1, 1) -dp.add(2, -1) +dp = [0] * (N + 1) +dp[1] = 1 +dp[2] = -1 +x = 0 -    x = dp.sum(i) +    x = (x + dp[i]) % mod -        dp.add(i + L[j], x) +        if i + L[j] > N: +            continue +        dp[i + L[j]] = (dp[i + L[j]] + x) % mod -            dp.add(i + R[j] + 1, -x) -print((dp.sum(N))) +            dp[i + R[j] + 1] = (dp[i + R[j] + 1] - x) % mod +x = (x + dp[N]) % mod +print(x)