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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;

fn main() {
    input! {
        n: usize,
        k: usize,
        lr: [(usize, usize); k],
    }

    let mut dp = vec![Mi::new(0); n + n + n];
    dp[0] = Mi::new(1);
    dp[1] = -Mi::new(1);

    for i in 0..n {
        let cur = dp[i];
        for &(l, r) in &lr {
            dp[i + l] += cur;
            dp[i + r + 1] -= cur;
        }
        dp[i + 1] += cur;

        // for &(l, r) in &lr {
        //     for j in l..=r {
        //         dp[i + j] += cur;
        //     }
        // }
    }

    println!("{}", dp[n - 1]);
}

type Mi = ModInt<Mod998>;

// /////////////////////////////////////////////////////////////
use std::fmt;
use std::marker::PhantomData;
use std::ops::*;

#[derive(Clone, Copy, Eq, PartialEq, fmt::Debug)]
struct ModInt<M: Mod> {
    n: i64,
    _p: PhantomData<M>,
}

impl<M: Mod> Add<ModInt<M>> for ModInt<M> {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        let mut n = self.n + other.n;
        if n < 0 {
            n += M::get();
        } else if n >= M::get() {
            n -= M::get();
        }
        Self { n, _p: PhantomData }
    }
}

impl<M: Mod> AddAssign<ModInt<M>> for ModInt<M> {
    fn add_assign(&mut self, other: Self) {
        self.n += other.n;
        if self.n < 0 {
            self.n += M::get();
        } else if self.n >= M::get() {
            self.n -= M::get();
        }
    }
}

impl<M: Mod> Sub<ModInt<M>> for ModInt<M> {
    type Output = Self;
    fn sub(self, other: Self) -> Self::Output {
        let mut n = self.n - other.n;
        if n < 0 {
            n += M::get();
        } else if n >= M::get() {
            n -= M::get();
        }
        Self { n, _p: PhantomData }
    }
}

impl<M: Mod> SubAssign<ModInt<M>> for ModInt<M> {
    fn sub_assign(&mut self, other: Self) {
        self.n -= other.n;
        if self.n < 0 {
            self.n += M::get();
        } else if self.n >= M::get() {
            self.n -= M::get();
        }
    }
}

impl<M: Mod> Mul<ModInt<M>> for ModInt<M> {
    type Output = Self;
    fn mul(self, other: Self) -> Self::Output {
        let n = (self.n * other.n).rem_euclid(M::get());
        Self { n, _p: PhantomData }
    }
}

impl<M: Mod> MulAssign<ModInt<M>> for ModInt<M> {
    fn mul_assign(&mut self, other: Self) {
        self.n = (self.n * other.n).rem_euclid(M::get());
    }
}

impl<M: Mod> Div<ModInt<M>> for ModInt<M> {
    type Output = Self;
    fn div(self, other: Self) -> Self::Output {
        let n = (self.n * other.recip().unwrap().n).rem_euclid(M::get());
        Self { n, _p: PhantomData }
    }
}

impl<M: Mod> DivAssign<ModInt<M>> for ModInt<M> {
    fn div_assign(&mut self, other: Self) {
        self.n = (self.n * other.recip().unwrap().n).rem_euclid(M::get());
    }
}

impl<M: Mod> Neg for ModInt<M> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        if self.n == 0 {
            Self {
                n: 0,
                _p: PhantomData,
            }
        } else {
            Self {
                n: M::get() - self.n,
                _p: PhantomData,
            }
        }
    }
}

impl<M: Mod> fmt::Display for ModInt<M> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.n)
    }
}

impl<M: Mod> ModInt<M> {
    fn new(n: i64) -> Self {
        let n = n.rem_euclid(M::get());
        Self { n, _p: PhantomData }
    }

    fn get(self) -> i64 {
        self.n
    }

    fn recip(self) -> Option<Self> {
        let mut x = 0i64;
        let mut y = 1i64;
        let mut a = self.n;
        let mut b = M::get();
        let mut u = y;
        let mut v = x;
        while a != 0 {
            let q = b / a;
            let tmp = x - q * u;
            x = u;
            u = tmp;
            let tmp = y - q * v;
            y = v;
            v = tmp;
            let tmp = b - q * a;
            b = a;
            a = tmp;
        }
        if b == 1 {
            Some(Self::new(x))
        } else {
            None
        }
    }

    fn pow(self, mut n: u64) -> Self {
        let mut res = ModInt::<M>::new(1);
        let mut dbl = self.clone();
        while n > 0 {
            if n & 1 == 1 {
                res *= dbl;
            }
            dbl *= dbl;
            n >>= 1;
        }
        res
    }
}

trait Mod: Copy + Clone + fmt::Debug {
    fn get() -> i64;
}

#[derive(Copy, Clone, Eq, PartialEq, fmt::Debug)]
struct Mod998 {}

impl Mod for Mod998 {
    fn get() -> i64 {
        998_244_353
    }
}

#[derive(fmt::Debug)]
struct ModValTable<M> {
    fact: Vec<M>,
    fact_inv: Vec<M>,
    inv: Vec<M>,
}

impl<M: Mod> ModValTable<ModInt<M>> {
    fn new(n: usize) -> Self {
        let mut fact = vec![ModInt::<M>::new(1); n + 1];
        let mut fact_inv = vec![ModInt::<M>::new(1); n + 1];
        let mut inv = vec![ModInt::<M>::new(1); n + 1];
        for i in 2..=n {
            fact[i] = fact[i - 1] * ModInt::<M>::new(i as i64);
            inv[i] = -ModInt::<M>::new(M::get() / i as i64)
                * inv[M::get() as usize % i];
            fact_inv[i] = fact_inv[i - 1] * inv[i];
        }

        Self {
            fact,
            fact_inv,
            inv,
        }
    }

    fn perm(&self, n: usize, r: usize) -> ModInt<M> {
        if n < r {
            ModInt::<M>::new(0)
        } else {
            self.fact[n] * self.fact_inv[n - r]
        }
    }

    fn binom(&self, n: usize, r: usize) -> ModInt<M> {
        if n < r {
            ModInt::<M>::new(0)
        } else {
            self.fact[n] * self.fact_inv[r] * self.fact_inv[n - r]
        }
    }

    fn mulnom(&self, n: usize, r: usize) -> ModInt<M> {
        self.binom(n + r - 1, r)
    }

    fn fact(&self, n: usize) -> ModInt<M> {
        self.fact[n]
    }

    fn fact_recip(&self, n: usize) -> ModInt<M> {
        self.fact_inv[n]
    }

    fn recip(&self, n: usize) -> ModInt<M> {
        self.inv[n]
    }
}
// /////////////////////////////////////////////////////////////