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)

#[allow(unused_imports)]
use proconio::{input, marker::*};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

use self::modint::ModInt;
mod modint {
    use std;
    #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt(usize);
    impl ModInt {
        pub fn new(value: usize) -> Self {
            ModInt(value % Self::modulo())
        }
        fn modulo() -> usize {
            998244353
        }
        pub fn pow(&self, n: usize) -> Self {
            let mut res = ModInt::new(1);
            let mut beki = *self;
            let mut n = n;
            while n > 0 {
                if n % 2 == 1 {
                    res *= beki;
                }
                beki *= beki;
                n /= 2;
            }
            res
        }
    }
    impl std::ops::Add for ModInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut value = self.0 + other.0;
            if value > Self::modulo() {
                value -= Self::modulo();
            }
            ModInt::new(value)
        }
    }
    impl std::ops::Add<usize> for ModInt {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self + other
        }
    }
    impl std::ops::Sub for ModInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut value = self.0;
            if value < other.0 {
                value += Self::modulo();
            }
            value -= other.0;
            ModInt::new(value)
        }
    }
    impl std::ops::Sub<usize> for ModInt {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self - other
        }
    }
    impl std::ops::Mul for ModInt {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let value = self.0 * other.0 % Self::modulo();
            ModInt::new(value)
        }
    }
    impl std::ops::Mul<usize> for ModInt {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self * other
        }
    }
    impl std::ops::Mul<ModInt> for usize {
        type Output = ModInt;
        fn mul(self, other: ModInt) -> Self::Output {
            let value = ModInt::new(self);
            value * other
        }
    }
    impl std::ops::Div for ModInt {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut exp = Self::modulo() - 2;
            let mut value = self.0;
            let mut ovalue = other.0;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % Self::modulo();
                }
                ovalue = ovalue * ovalue % Self::modulo();
                exp /= 2;
            }
            ModInt::new(value)
        }
    }
    impl std::ops::Div<usize> for ModInt {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            let other = ModInt::new(other);
            self / other
        }
    }
    impl std::ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }
    impl std::ops::AddAssign<usize> for ModInt {
        fn add_assign(&mut self, other: usize) {
            *self = *self + other;
        }
    }
    impl std::ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }
    impl std::ops::SubAssign<usize> for ModInt {
        fn sub_assign(&mut self, other: usize) {
            *self = *self - other;
        }
    }
    impl std::ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }
    impl std::ops::MulAssign<usize> for ModInt {
        fn mul_assign(&mut self, other: usize) {
            *self = *self * other;
        }
    }
    impl std::ops::DivAssign for ModInt {
        fn div_assign(&mut self, other: Self) {
            *self = *self / other;
        }
    }
    impl std::ops::DivAssign<usize> for ModInt {
        fn div_assign(&mut self, other: usize) {
            *self = *self / other;
        }
    }
    impl std::fmt::Debug for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
    impl std::fmt::Display for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
}

pub trait Monoid<T>: SemiGroup<T> {
    fn identity() -> T;
}
pub trait SemiGroup<T>: Magma<T> {}
pub trait Magma<T> {
    fn operate(lhs: T, rhs: T) -> T;
}
#[derive(Debug)]
struct SegmentTree<T, U> {
    len: usize,
    nodes: Vec<T>,
    phantom: std::marker::PhantomData<U>,
}
impl<T, U> SegmentTree<T, U>
where
    U: Monoid<T>,
    T: Copy + Clone + std::fmt::Debug,
{
    #[allow(dead_code)]
    fn new(n: usize) -> Self {
        let mut len = 1;
        while len < n {
            len *= 2;
        }
        SegmentTree {
            len: len,
            nodes: vec![U::identity(); 2 * len - 1],
            phantom: Default::default(),
        }
    }
    #[allow(dead_code)]
    fn from_vec(vec: &Vec<T>) -> Self {
        let mut len = 1;
        while len < vec.len() {
            len *= 2;
        }
        let mut nodes = vec![U::identity(); 2 * len - 1];
        for i in 0..vec.len() {
            nodes[i + len - 1] = vec[i];
        }
        for i in (0..len - 1).rev() {
            nodes[i] = U::operate(nodes[2 * i + 1], nodes[2 * i + 2]);
        }
        SegmentTree {
            len: len,
            nodes: nodes,
            phantom: Default::default(),
        }
    }
    #[allow(dead_code)]
    fn update(&mut self, index: usize, val: T) {
        let mut index = index;
        index += self.len - 1;
        self.nodes[index] = val;
        while index > 0 {
            index = (index - 1) / 2;
            self.nodes[index] = U::operate(self.nodes[2 * index + 1], self.nodes[2 * index + 2]);
        }
    }
    fn get(&self, l: usize, r: usize) -> T {
        self._get(l, r, 0, 0, self.len)
    }
    fn _get(&self, ql: usize, qr: usize, index: usize, l: usize, r: usize) -> T {
        if r <= ql || qr <= l {
            return U::identity();
        }
        if ql <= l && r <= qr {
            return self.nodes[index];
        }
        let vl = self._get(ql, qr, 2 * index + 1, l, (l + r) / 2);
        let vr = self._get(ql, qr, 2 * index + 2, (l + r) / 2, r);
        U::operate(vl, vr)
    }
}

struct IMonoid;
impl Monoid<ModInt> for IMonoid {
    fn identity() -> ModInt {
        ModInt::new(0)
    }
}
impl Magma<ModInt> for IMonoid {
    fn operate(lhs: ModInt, rhs: ModInt) -> ModInt {
        lhs + rhs
    }
}
impl SemiGroup<ModInt> for IMonoid {}

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

    let mut dp = SegmentTree::<ModInt, IMonoid>::new(n + 2);
    dp.update(1, ModInt::new(1));

    for i in 2..n + 1 {
        let mut sum = ModInt::new(0);
        for &(l, r) in &lr {
            if l > i {
                continue;
            } else if r > i {
                sum += dp.get(1, i - l + 1);
            } else {
                sum += dp.get(i - r, i - l + 1);
            }
        }
        dp.update(i, sum);
    }
    println!("{}", dp.get(n, n + 1));
}