Problem:
Score : 400 points

Problem Statement

There are N cells arranged in a row, numbered 1, 2, \ldots, N from left to right.

Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.

You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L_1, R_1], [L_2, R_2], \ldots, [L_K, R_K].
Let S be the union of these K segments.
Here, the segment [l, r] denotes the set consisting of all integers i that satisfy l \leq i \leq r.

When you are on Cell i, pick an integer d from S and move to Cell i + d. You cannot move out of the cells.

To help Tak, find the number of ways to go to Cell N, modulo 998244353.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq K \leq \min(N, 10)

1 \leq L_i \leq R_i \leq N

[L_i, R_i] and [L_j, R_j] do not intersect (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
L_1 R_1
L_2 R_2
:
L_K R_K

Output

Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.

Sample Input 1

5 2
1 1
3 4

Sample Output 1

4

The set S is the union of the segment [1, 1] and the segment [3, 4], therefore S = \{ 1, 3, 4 \} holds.

There are 4 possible ways to get to Cell 5:

1 \to 2 \to 3 \to 4 \to 5,

1 \to 2 \to 5,

1 \to 4 \to 5 and

1 \to 5.

Sample Input 2

5 2
3 3
5 5

Sample Output 2

0

Because S = \{ 3, 5 \} holds, you cannot reach to Cell 5.
Print 0.

Sample Input 3

5 1
1 2

Sample Output 3

5

Sample Input 4

60 3
5 8
1 3
10 15

Sample Output 4

221823067

Note that you have to print the answer modulo 998244353.

Solution in C# (Mono-csc 3.5.0) (judge verdict: Time Limit Exceeded)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;
using System.Numerics;using static System.Math;
namespace _NS
{
    class _PR
    {
        static void Main()
        {
            var NK = CinTAr;
            int n = NK[0];
            int k = NK[1];
            Pair<int>[] LRS = new Pair<int>[k];
            AFOR(k, i =>
            {
                var lr = CinTAr;
                LRS[i] = new Pair<int>(lr[0], lr[1]);
            });
            Array.Sort(LRS, (x, y) => x.a - y.a);
            long[] dp = new long[n + 1];
            dp[1] = 1;
            for(int i = 1;i <= n; i++)
            {
                if(i > 1 && dp[i] == 0) { continue; }
                for(int g = 0; g < k; g++)
                {
                    var lr = LRS[g];
                    for(int j = lr.a; j<= lr.b; j++)
                    {
                        if(i + j > n) { break; }
                        dp[i + j] += dp[i];
                        dp[i + j] %= 998244353;
                    }
                    if(lr.b + i > n) { break; }
                }
            }
            Cout = dp[n] % 998244353;
            Wait();
        } //main
        #region LIBRARY
        static int MOD10_9_7 = 1000000000 + 7;
        static string Yes = "Yes", No = "No", Good = "Good", Bad = "Bad";
        static int GrARXY(int x, int y, int wid) { return y * wid + x; }
        static int[] GrXY(int arxy, int wid) { return new int[] { arxy % wid , arxy / wid}; }
        static int GetDigits(long n) { return (int)Floor(Log10(n)) + 1; }
        static int GetNumByDigit(long n, int d) { return (int)((n / ((int)Pow(10, d - 1))) % 10); }
        static long Loop(long n, long max){ if (n > max) { return n % (max + 1); } else if (n < 0) { return max - (-n - 1) % (max + 1); } else { return n; }}
        static int Loop(int n, int max) { return (int)Loop((long)n, max); }
        static long PW10(int n) { return (long)Pow(10, n); }
        static long GCD(long a, long b) { if (a < b) { return GCD(b, a); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } }
        static long LCM(long a, long b) { return a * b / GCD(a, b); }
        static void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
        static void SwapArE<T>(T[] a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static void SwapLiE<T>(List<T> a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static bool IsInt(double d) { return (long)d == d; }
        static string Wait() { return Console.ReadLine(); }
        static string Cin { get { return Wait(); } }
        static string[] CinAr { get { return Cin.Split(' ').Where(x => x != "").ToArray(); } }
        static int CinT { get { return S2I(Cin); } }
        static int[] CinTAr { get { return CinAr.Select(x => S2I(x)).ToArray(); } }
        static long CinL { get { return S2L(Cin); } }
        static long[] CinLAr { get { return CinAr.Select(x => S2L(x)).ToArray(); } }
        static double CinD { get { return S2D(Cin); } }
        static double[] CinDAr { get { return CinAr.Select(x => S2D(x)).ToArray(); } }
        static int S2I(string s) { return int.Parse(s); }
        static long S2L(string s) { return long.Parse(s); }
        static double S2D(string s) { return double.Parse(s); }
        static object Cout { set { Console.WriteLine(value); } }
        static object WCout { set { Console.Write(value); } }
        static string RevStr(string s) { return new string(s.Reverse().ToArray()); }
        static void AFOR(int m, Action<int> ActI) { for (int i = 0; i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action<long> ActI) { for (long i = 0; i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action Act) { AFOR(m, i => Act()); }
        static long[] Divisor(long n)
        {
            if (n == 1) { return new long[] { 1 }; } List<long> r = new List<long>() { 1, n };
            for (long i = 2; i <= Sqrt(n); i++) { if (n % i == 0) { r.Add(i); long f = n / i; if (f != i) { r.Add(f); } } } return r.ToArray();
        }
        static IEnumerable<decimal> PrimeFactors(decimal n)
        {
            decimal i = 2, t = n; while (i * i <= n) { if (t % i == 0) { t /= i; yield return i; } else { i++; } } if (t != 1) { yield return t; }
        }
        static bool IsPrime(long n){ if (n == 1) { return false; } for (long i = 2; i <= Sqrt(n) + 1; i++) { if (n % i == 0) { return false; } } return true;  }
        static void CoutPaAr<Type>(IEnumerable<Pair<Type>> p) { CoutAr(p.Select(x => $"({x.a}, {x.b}, {x.c})")); }
        static void CoutAr<Type>(IEnumerable<Type> t) { Cout = string.Join(" ", t); }
        static long nCr(int n, int r) { return nPr(n, r) / Factorial(r); }
        static long nPr(int n, int r) { return FactorDiv(n, n - r); }
        static long FactorDiv(int t, int d) { long r = 1; for (int i = t; i > d; i--) { r *= i; } return r; }
        static long Factorial(int i) { if (i <= 1) { return 1; } return i * Factorial(i - 1); }
        #endregion
    }
    struct Pair<T>
    {
        public T a, b, c;
        public Pair(T _a, T _b, T _c = default(T)) { a = _a; b = _b; c = _c; }
    }
}