Problem:
Score : 600 points

Problem Statement

There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.

Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.

Choose a robot and activate it. This operation cannot be done when there is a robot moving.

While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.

How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.

Constraints

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

-10^9 \leq X_i \leq 10^9

1 \leq D_i \leq 10^9

X_i \neq X_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 D_1
:
X_N D_N

Output

Print the number of possible sets of robots remaining on the number line, modulo 998244353.

Sample Input 1

2
1 5
3 3

Sample Output 1

3

There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.

These can be achieved as follows:

If Takahashi activates nothing, the robots \{1, 2\} will remain.

If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.

If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.

Sample Input 2

3
6 5
-1 10
3 3

Sample Output 2

5

There are five possible sets of robots remaining on the number line: \{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, and \{\}.

Sample Input 3

4
7 10
-10 3
4 3
-4 3

Sample Output 3

16

None of the robots influences others.

Sample Input 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

Sample Output 4

110

Remember to print the count modulo 998244353.

Solution in Rust (1.15.1) (judge verdict: Accepted)

// use rand::{thread_rng, Rng};

const MOD: usize = 998244353;
fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    // loop {
    //     let n = 4;
    //     let mut robots = vec![];
    //     let mut rng = thread_rng();
    //     for _ in 0..n {
    //         let x = rng.gen::<u8>() as i64;
    //         let d = rng.gen::<u8>() as i64 + 1;
    //         robots.push((x, d));
    //     }
    //     robots.sort_by_key(|r| r.0);
    //
    //     let ans = solve(&robots);
    //     let naive = naive(&robots);
    //     assert_eq!(ans, naive, "ans:{} naive:{} {:?}", ans, naive, robots);
    // }

    let n: usize = sc.read();
    let mut robots: Vec<(i64, i64)> = (0..n).map(|_| (sc.read(), sc.read())).collect();
    robots.sort_by_key(|r| r.0);
    let ans = solve(&robots);
    // let naive = naive(&robots);
    // assert_eq!(ans, naive, "ans:{} naive:{} {:?}", ans, naive, robots);
    println!("{}", ans);
}

fn solve(robots: &Vec<(i64, i64)>) -> usize {
    let n = robots.len();
    let mut dp = vec![[0, 0]; n];
    let mut seg = SegmentTree::new(n, 0, std::cmp::max);
    seg.update(n - 1, n - 1);
    dp[n - 1][0] = 1;
    dp[n - 1][1] = 1;
    for i in (0..(n - 1)).rev() {
        let touch = robots[i].0 + robots[i].1;
        let upper = robots
            .binary_search_by_key(&(touch * 2 - 1), |r| r.0 * 2)
            .err()
            .unwrap();
        assert!(upper == n || touch <= robots[upper].0);
        dp[i][0] = (dp[i + 1][0] + dp[i + 1][1]) % MOD;

        if upper <= i + 1 {
            dp[i][1] = (dp[i + 1][0] + dp[i + 1][1]) % MOD;
            seg.update(i, i);
        } else {
            let next = seg.query(i + 1, upper);
            seg.update(i, next);
            dp[i][1] = dp[next][1];
        }
    }
    (dp[0][0] + dp[0][1]) % MOD
}

fn naive(robots: &Vec<(i64, i64)>) -> usize {
    let n = robots.len();
    let mut set = std::collections::BTreeSet::new();
    for mask in 0..(1 << n) {
        let mut dead = 0;
        let mut cur_min = std::i64::MIN;
        for i in 0..n {
            if mask & (1 << i) != 0 || robots[i].0 < cur_min {
                cur_min = std::cmp::max(cur_min, robots[i].0 + robots[i].1);
            }
            if robots[i].0 < cur_min {
                dead += 1 << i;
            }
        }
        set.insert(dead);
    }
    set.len()
}
pub struct SegmentTree<T, F> {
    seg: Vec<T>,
    n: usize,
    f: F,
    initial_value: T,
}

impl<T: Copy, F> SegmentTree<T, F>
where
    F: Fn(T, T) -> T,
{
    pub fn new(size: usize, initial_value: T, f: F) -> SegmentTree<T, F> {
        let mut m = 1;
        while m <= size {
            m <<= 1;
        }
        SegmentTree {
            seg: vec![initial_value; m * 2],
            n: m,
            f: f,
            initial_value: initial_value,
        }
    }

    pub fn update(&mut self, k: usize, value: T) {
        let mut k = k;
        k += self.n - 1;
        self.seg[k] = value;
        while k > 0 {
            k = (k - 1) >> 1;
            self.seg[k] = (self.f)(self.seg[k * 2 + 1], self.seg[k * 2 + 2]);
        }
    }

    /// Get the minimum value in the array in the range [a, b)
    ///
    /// # Panics
    ///
    /// Panics if `a >= b`.
    pub fn query(&self, a: usize, b: usize) -> T {
        assert!(a < b);
        self.query_range(a, b, 0, 0, self.n)
    }

    fn query_range(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            self.initial_value
        } else if a <= l && r <= b {
            self.seg[k]
        } else {
            let x = self.query_range(a, b, k * 2 + 1, l, (l + r) >> 1);
            let y = self.query_range(a, b, k * 2 + 2, (l + r) >> 1, r);
            (self.f)(x, y)
        }
    }
}
pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}