
Imports System.Console
Module Module1
    Dim w As Int64
    Dim ii, iii As Int64
    Dim ans As Int64
    Dim md As Int64 = 998244353
    Dim max, min As Int64
    Dim n, k, x, t, d, m As Int64
    Sub Add(ByRef arr() As Integer, ByVal num As Integer)
        If arr Is Nothing Then
            ReDim arr(0)
            arr(0) = num
        Else
            ReDim Preserve arr(arr.Length)
            arr(arr.Length - 1) = num
        End If
    End Sub
    Sub Main()
        Dim s() As String = ReadLine.Split
        n = s(0)
        k = s(1)
        'Dim can() As Integer
        'Dim b(n) As Boolean
        Dim s1(k), s2(k) As Integer
        For i = 1 To k
            s = ReadLine.Split
            'For ii = s(0) To s(1)
            '    b(ii) = True
            'Next
            s1(i) = s(0)
            s2(i) = s(1)
        Next
        'For i = 1 To n
        '    If b(i) Then
        '        Add(can, i)
        '    End If
        'Next
        Dim dp(n) As Int64
        dp(1) = 1
        Dim a As Integer
        Dim wa(k), wa1(k), wa2(k) As Integer
        For i = 1 To k
            wa1(i) = 1 - s1(i)
            wa2(i) = 1 - s2(i)
        Next
        For i = 1 To n
            For ii = 1 To k
                dp(i) += wa(ii)
            Next
            For ii = 1 To k
                'If s2(ii) < i Then
                '    For iii = s1(ii) To s2(ii)
                '        dp(i) += dp(i - iii)
                '    Next
                'ElseIf s1(ii) < i Then
                '    For iii = s1(ii) To i - 1
                '        dp(i) += dp(i - iii)
                '    Next
                'End If
                wa1(ii) += i
                wa2(ii) += i
                If wa2(ii) > 0 Then
                    wa(ii) += dp(wa2(ii))
                    If wa1(ii) > 1 Then
                        wa(ii) -= dp(wa1(ii) - 1)
                    End If
                End If
                wa1(ii) -= i
                wa2(ii) -= i
            Next
            dp(i) = dp(i) Mod md
            'Do While a < can.Length AndAlso can(a) < i
            '    w = can(a)
            '    dp(i) += dp(i - w)
            '    a += 1
            'Loop
            'dp(i) = dp(i) Mod md
            'a = 0
            'w = 0

            'For ii = 1 To i
            '    If can.Contains(i - ii) Then
            '        dp(i) += dp(ii)
            '        dp(i) = dp(i) Mod md
            '    End If
            'Next
        Next
        'Dim st As String = ""
        'For i = 0 To can.Length - 1
        '    st &= " " & can(i)
        'Next
        'WriteLine(st)
        WriteLine(dp(n))
        ReadLine()
    End Sub
End Module
