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 std::io::*;
const MOD: u64 = 998244353;

mod segment_tree {
    pub struct SegmentTree<T, F> {
        size: usize,
        data: Vec<T>,
        identity: T,
        operation: F,
    }
    impl<T: Copy + Clone, F: Fn(T, T) -> T> SegmentTree<T, F> {
        pub fn new(n: usize, id: T, op: F) -> SegmentTree<T, F> {
            let mut size = 1;
            while size < n {
                size <<= 1;
            }
            SegmentTree {
                size: size,
                data: vec![id; size * 2],
                identity: id,
                operation: op,
            }
        }
        pub fn update(&mut self, mut i: usize, x: T) {
            i += self.size;
            self.data[i] = x;
            i >>= 1;
            while i > 0 {
                self.data[i] = (self.operation)(self.data[i * 2], self.data[i * 2 + 1]);
                i >>= 1;
            }
        }
        pub fn query(&self, mut l: usize, mut r: usize) -> T {
            let mut a = self.identity;
            let mut b = self.identity;
            l += self.size;
            r += self.size;
            while l < r {
                if (l & 1) == 1 {
                    a = (self.operation)(a, self.data[l]);
                    l += 1;
                }
                if (r & 1) == 1 {
                    r -= 1;
                    b = (self.operation)(self.data[r], b);
                }
                l >>= 1;
                r >>= 1;
            }
            (self.operation)(a, b)
        }
    }
}

fn lower_bound<T: PartialOrd>(x: T, a: &Vec<T>) -> usize {
    let mut l: usize = 0;
    let mut r: usize = a.len();
    while r != l {
        let mid = (l + r) >> 1;
        if a[mid] < x {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    l
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let mut a: Vec<(i64, i64)> = (0..n)
        .map(|_| {
            (
                itr.next().unwrap().parse().unwrap(),
                itr.next().unwrap().parse().unwrap(),
            )
        })
        .collect();
    a.sort();

    let mut x: Vec<i64> = vec![0; n];
    let mut d: Vec<i64> = vec![0; n];
    for i in 0..n {
        x[i] = a[i].0;
        d[i] = a[i].1;
    }
    // dp: iを選ぶ時、選ばない時で遷移
    // 選ばない時はi+1に遷移
    // iを選んだ時は同時に動く最大のもの+1に遷移
    // 同時に動く最大のもの: 再帰的に動きは起こるので、後ろから決めていく
    let mut st = segment_tree::SegmentTree::new(n, 0, std::cmp::max);
    let mut to: Vec<usize> = (0..n).collect();
    for i in (0..n).rev() {
        let j = lower_bound(x[i] + d[i], &x);
        if j > i + 1 {
            to[i] = st.query(i, j);
        }
        st.update(i, to[i]);
    }

    let mut dp: Vec<u64> = vec![0; n + 1];
    dp[0] = 1;
    for i in 0..n {
        dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
        dp[to[i] + 1] = (dp[to[i] + 1] + dp[i]) % MOD;
    }
    println!("{}", dp[n]);
}