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)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 998244353;

/// 降順 xs について z 未満の最小添字を返す
fn bisearh(xs: &Vec<(i64, i64)>, z: i64) -> usize {
    let n = xs.len();
    if xs[0].0 < z { return 0 }
    let mut left = 0;  // xs[left] >= z
    let mut right = n;  // xs[right] < z
    for _ in 0..100 {
        let mid = (left + right) / 2;
        if xs[mid].0 >= z {
            left = mid;
        } else {
            right = mid;
        }
    }
    right
}

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let mut rs: Vec<_> = (0..n).map(|_| {
        let x: i64 = sc.cin();
        let d: i64 = sc.cin();
        (x, d)
    }).collect();
    rs.sort(); rs.reverse();
    // trace!(&rs);

    // st[i] = i 番目を起動した場合に起動する最小の番目
    let mut st = SegmentTree::new(n);
    st.update(0, MinInt::Val(0));
    {
        for i in 1..n {
            st.update(i, MinInt::Val(i));
            let (x, d) = rs[i];
            let k = bisearh(&rs, x + d);
            if let MinInt::Val(l) = st.product(k..i + 1) {
                st.update(i, MinInt::Val(l));
            }
            // trace!(i, k, st[i]);
        }
    }

    // dp[i][0] = i を起動しない 場合の 0..i の動かし方
    // dp[i][1] = i を起動する   場合の 0..i の動かし方
    let mut dp = vec![vec![ModInt(0, M); 2]; n];
    dp[0][0] += 1;
    dp[0][1] += 1;
    for i in 1..n {
        dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
        let k = st[i].unwrap();
        dp[i][1] = if k == 0 {
            ModInt(1, M)
        } else {
            dp[k - 1][0] + dp[k - 1][1]
        };
    }

    put!(dp[n-1][0] + dp[n-1][1]);

}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
struct ModInt(i64, i64);  // (residual, modulo)
impl ModInt {
    fn new(residual: i64, modulo: i64) -> ModInt {
        if residual >= modulo { ModInt(residual % modulo, modulo) }
        else if residual < 0 { ModInt((residual % modulo) + modulo, modulo) }
        else { ModInt(residual, modulo) }
    }
    fn unwrap(self) -> i64 { self.0 }
    fn inv(self) -> Self {
        fn exgcd(r0: i64, a0: i64, b0: i64, r: i64, a: i64, b: i64)
                -> (i64, i64, i64) {
            if r > 0 {
                exgcd(r, a, b, r0 % r, a0 - r0 / r * a, b0 - r0 / r * b)
            } else {
                (a0, b0, r0)
            }
        }
        let (a, _, r) = exgcd(self.0, 1, 0, self.1, 0, 1);
        if r != 1 { panic!("{:?} has no inverse!", self); }
        ModInt(((a % self.1) + self.1) % self.1, self.1)
    }
    fn pow(self, n: i64) -> Self {
        if n < 0 {
            self.pow(-n).inv()
        } else if n == 0 {
            ModInt(1, self.1)
        } else if n == 1 {
            self
        } else {
            let mut x = (self * self).pow(n / 2);
            if n % 2 == 1 { x *= self }
            x
        }
    }
}
impl std::fmt::Display for ModInt {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Neg for ModInt {
    type Output = Self;
    fn neg(self) -> Self {
        if self.0 == 0 { return self }
        ModInt(self.1 - self.0, self.1)
    }
}
impl std::ops::Add<i64> for ModInt {
    type Output = Self;
    fn add(self, other: i64) -> Self { ModInt::new(self.0 + other, self.1) }
}
impl std::ops::Add for ModInt {
    type Output = Self;
    fn add(self, other: ModInt) -> Self { self + other.0 }
}
impl std::ops::Add<ModInt> for i64 {
    type Output = ModInt;
    fn add(self, other: ModInt) -> ModInt { other + self }
}
impl std::ops::AddAssign<i64> for ModInt {
    fn add_assign(&mut self, other: i64) { self.0 = ModInt::new(self.0 + other, self.1).0; }
}
impl std::ops::AddAssign for ModInt {
    fn add_assign(&mut self, other: ModInt) { *self += other.0; }
}
impl std::ops::Sub<i64> for ModInt {
    type Output = Self;
    fn sub(self, other: i64) -> Self { ModInt::new(self.0 - other, self.1) }
}
impl std::ops::Sub for ModInt {
    type Output = Self;
    fn sub(self, other: ModInt) -> Self { self - other.0 }
}
impl std::ops::Sub<ModInt> for i64 {
    type Output = ModInt;
    fn sub(self, other: ModInt) -> ModInt { ModInt::new(self - other.0, other.1) }
}
impl std::ops::SubAssign<i64> for ModInt {
    fn sub_assign(&mut self, other: i64) { self.0 = ModInt::new(self.0 - other, self.1).0; }
}
impl std::ops::SubAssign for ModInt {
    fn sub_assign(&mut self, other: ModInt) { *self -= other.0; }
}
impl std::ops::Mul<i64> for ModInt {
    type Output = Self;
    fn mul(self, other: i64) -> Self { ModInt::new(self.0 * other, self.1) }
}
impl std::ops::Mul for ModInt {
    type Output = Self;
    fn mul(self, other: ModInt) -> Self { self * other.0 }
}
impl std::ops::Mul<ModInt> for i64 {
    type Output = ModInt;
    fn mul(self, other: ModInt) -> ModInt { other * self }
}
impl std::ops::MulAssign<i64> for ModInt {
    fn mul_assign(&mut self, other: i64) { self.0 = ModInt::new(self.0 * other, self.1).0; }
}
impl std::ops::MulAssign for ModInt {
    fn mul_assign(&mut self, other: ModInt) { *self *= other.0; }
}
impl std::ops::Div for ModInt {
    type Output = Self;
    fn div(self, other: ModInt) -> Self { self * other.inv() }
}
impl std::ops::Div<i64> for ModInt {
    type Output = Self;
    fn div(self, other: i64) -> Self { self / ModInt(other, self.1) }
}
impl std::ops::Div<ModInt> for i64 {
    type Output = ModInt;
    fn div(self, other: ModInt) -> ModInt { other.inv() * self }
}
impl std::ops::DivAssign for ModInt {
    fn div_assign(&mut self, other: ModInt) { self.0 = (self.clone() / other).0; }
}
impl std::ops::DivAssign<i64> for ModInt {
    fn div_assign(&mut self, other: i64) { *self /= ModInt(other, self.1); }
}

trait Monoid: std::ops::Mul<Output=Self> + Clone + Copy {
    fn unit() -> Self;
}
impl Monoid for i64 { fn unit() -> Self { 0 }}
impl Monoid for f64 { fn unit() -> Self { 0.0 }}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum MinInt { Val(usize), Maximal }
impl MinInt { fn unwrap(self) -> usize { if let MinInt::Val(x) = self { x } else { panic!(); } } }
impl std::ops::Mul for MinInt {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        if self < other { self } else { other }
    }
}
impl Monoid for MinInt {
    fn unit() -> Self { MinInt::Maximal }
}

#[derive(Clone)]
struct SegmentTree<T> { size: usize, depth: usize, data: Vec<Vec<T>> }
impl<T> std::ops::Index<usize> for SegmentTree<T> {
    type Output = T;
    fn index(&self, i: usize) -> &Self::Output {
        &self.data[self.depth - 1][i]
    }
}
impl<T: Monoid> SegmentTree<T> {
    fn new(size: usize) -> SegmentTree<T> {
        let mut data = vec![];
        let mut i = 1;
        loop {
            data.push(vec![T::unit(); i]);
            if i >= size { break }
            i *= 2;
        }
        SegmentTree { size: size, depth: data.len(), data: data }
    }
    fn to_vec(self) -> Vec<T> {
        (self.data[self.depth - 1][0..self.size]).to_vec()
    }
    fn update(&mut self, idx: usize, t: T) {
        self.data[self.depth - 1][idx] = t;
        let mut i = idx;
        for d in (0..self.depth - 1).rev() {
            i /= 2;
            self.data[d][i] = self.data[d + 1][i * 2] * self.data[d + 1][i * 2 + 1];
        }
    }
    fn product(&self, range: std::ops::Range<usize>) -> T {
        if range.end == 0 || range.start + 1 > range.end {
            T::unit()
        } else if range.start + 1 == range.end {
            self[range.start]
        } else {
            let mut i = range.start;
            let mut d = self.depth - 1;
            let mut width = 1;
            while d > 0 {
                let left = (i / 2) * width * 2;
                let right = left + width * 2;
                if left == range.start && right <= range.end {
                    i /= 2;
                    d -= 1;
                    width *= 2;
                } else {
                    break
                }
            }
            let right = range.start + width;
            self.data[d][i] * self.product(right..range.end)
        }
    }
}
impl<T: std::fmt::Debug> SegmentTree<T> {
    fn debug(&self) {
        for i in 0..self.depth {
            trace!(&self.data[i]);
        }
    }
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}