Problem:
Score : 500 points

Problem Statement

You have a string S of length N.
Initially, all characters in S are 1s.

You will perform queries Q times.
In the i-th query, you are given two integers L_i, R_i and a character D_i (which is a digit).
Then, you must replace all characters from the L_i-th to the R_i-th (inclusive) with D_i.

After each query, read the string S as a decimal integer, and print its value modulo 998,244,353.

Constraints

1 \leq N, Q \leq 200,000

1 \leq L_i \leq R_i \leq N

1 \leq D_i \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
L_1 R_1 D_1
:
L_Q R_Q D_Q

Output

Print Q lines.
In the i-th line print the value of S after the i-th query, modulo 998,244,353.

Sample Input 1

8 5
3 6 2
1 4 7
3 8 3
2 2 2
4 5 1

Sample Output 1

11222211
77772211
77333333
72333333
72311333

Sample Input 2

200000 1
123 456 7

Sample Output 2

641437905

Don't forget to take the modulo.

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

use std::fmt::Debug;
use std::str::FromStr;

pub struct TokenReader {
    reader: std::io::Stdin,
    tokens: Vec<String>,
    index: usize,
}

impl TokenReader {
    pub fn new() -> Self {
        Self {
            reader: std::io::stdin(),
            tokens: Vec::new(),
            index: 0,
        }
    }

    pub fn next<T>(&mut self) -> T
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index += 1;
        self.tokens[self.index - 1].parse().unwrap()
    }

    pub fn vector<T>(&mut self) -> Vec<T>
    where
        T: FromStr,
        T::Err: Debug,
    {
        if self.index >= self.tokens.len() {
            self.load_next_line();
        }
        self.index = self.tokens.len();
        self.tokens.iter().map(|tok| tok.parse().unwrap()).collect()
    }

    pub fn load_next_line(&mut self) {
        let mut line = String::new();
        self.reader.read_line(&mut line).unwrap();

        self.tokens = line
            .split_whitespace()
            .map(String::from)
            .collect();
        self.index = 0;
    }
}

const MOD: i64 = 998244353;

struct Ones {
    total: Vec<i64>,
}

impl Ones {
    fn new(size: usize) -> Self {
        let mut total = vec![1; size];
        for i in 1..size {
            total[i] = (total[i - 1] * 10 + 1) % MOD;
        }

        Self { total }
    }

    fn num(&self, digit: i64, left: usize, right: usize) -> i64 {
        let mut ones = self.total[right];
        if left > 0 {
            ones = (ones + MOD - self.total[left - 1]) % MOD;
        }
        (ones * digit) % MOD
    }
}

struct SegTree {
    sum: Vec<i64>,
    lazy: Vec<i64>,
    ones: Ones,
    size: usize,
}

impl SegTree {
    fn new(size: usize) -> Self {
        let nodes = 4 * size + 10;
        Self {
            sum: vec![0; nodes],
            lazy: vec![0; nodes],
            ones: Ones::new(size + 1),
            size,
        }
    }

    fn update(&mut self, digit: i64, left: usize, right: usize) {
        self.up(0, 0, self.size - 1, digit, left, right)
    }

    fn up(&mut self, node: usize, l: usize, r: usize, digit: i64, left: usize, right: usize) {
        self.propagate(node, l, r);

        if left <= l && r <= right {
            self.lazy[node] = digit;
            self.propagate(node, l, r);
            return;
        }

        let m = l + (r - l) / 2;
        if left <= m {
            self.up(self.left(node), l, m, digit, left, right);
        }
        if m < right {
            self.up(self.right(node), m + 1, r, digit, left, right);
        }

        self.propagate(self.left(node), l, m);
        self.propagate(self.right(node), m + 1, r);

        self.sum[node] =
            (self.sum[self.left(node)] + self.sum[self.right(node)]) % MOD;
    }

    fn get(&mut self, left: usize, right: usize) -> i64 {
        self.find(0, 0, self.size - 1, left, right)
    }

    fn find(&mut self, node: usize, l: usize, r: usize, left: usize, right: usize) -> i64 {
        self.propagate(node, l, r);

        if left <= l && r <= right {
            return self.sum[node];
        }

        let m = l + (r - l) / 2;
        let mut res = 0;

        if left <= m {
            res = (res + self.find(self.left(node), l, m, left, right)) % MOD;
        }
        if m < right {
            res = (res + self.find(self.right(node), m + 1, r, left, right)) % MOD;
        }
        res
    }

    fn propagate(&mut self, node: usize, left: usize, right: usize) {
        let l = self.left(node);
        if l < self.sum.len() && self.lazy[node] > 0 {
            self.lazy[l] = self.lazy[node];
        }

        let r = self.right(node);
        if r < self.sum.len() && self.lazy[node] > 0 {
            self.lazy[r] = self.lazy[node];
        }

        if self.lazy[node] > 0 {
            self.sum[node] = self.ones.num(self.lazy[node], left, right);
        }
        self.lazy[node] = 0;
    }

    fn left(&self, node: usize) -> usize {
        2 * node + 1
    }

    fn right(&self, node: usize) -> usize {
        2 * node + 2
    }
}

fn main() {
    let mut reader = TokenReader::new();
    let n = reader.next();
    let q = reader.next();

    let mut num = SegTree::new(n);
    num.update(1, 0, n - 1);

    for _ in 0..q {
        let r = n - reader.next::<usize>();
        let l = n - reader.next::<usize>();
        let d = reader.next();

        num.update(d, l, r);
        println!("{}", num.get(0, n - 1));
    }
}