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()

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()

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()

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()

-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) - - -    bit = BIT(N + 2) -    bit.add(1, 1) -    bit.add(2, -1) -    for i in range(N): -        cur = bit.get_sum(i) % MOD +    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] -            bit.add(i + l, cur) -            bit.add(i + r + 1, -cur) -    print((bit.get_sum(N) % MOD)) +            if i + l <= N: +                dp[i + l] += cur +                dp[min(i + r + 1, N + 1)] -= cur +    print((dp[N]))