Problem:
Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

0 l r b c: For each i = l, l+1, \dots, {r - 1}, set a_i \gets b \times a_i + c.

1 l r: Print \sum_{i = l}^{r - 1} a_i \bmod 998244353.

Constraints

1 \leq N, Q \leq 500000

0 \leq a_i, c < 998244353

1 \leq b < 998244353

0 \leq l < r \leq N

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
a_0 a_1 ... a_{N - 1}
\textrm{Query}_0
\textrm{Query}_1
:
\textrm{Query}_{Q - 1}

Output

For each query of the latter type, print the answer.

Sample Input 1

5 7
1 2 3 4 5
1 0 5
0 2 4 100 101
1 0 3
0 1 3 102 103
1 2 5
0 2 5 104 105
1 0 5

Sample Output 1

15
404
41511
4317767

Solution in Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	q := getNextInt(scanner)
	ss := make([]SInterface, n)
	for i := 0; i < n; i++ {
		a := getNextInt(scanner)
		ss[i] = &S{a: mint(a), size: 1}
	}
	lzt := NewLazySegTreeWithData(ss, E, ID)
	for i := 0; i < q; i++ {
		t := getNextInt(scanner)
		if t == 0 {
			l := getNextInt(scanner)
			r := getNextInt(scanner)
			b := getNextInt(scanner)
			c := getNextInt(scanner)
			lzt.ApplyRange(l, r, &F{a: mint(b), b: mint(c)})
			continue
		}
		l := getNextInt(scanner)
		r := getNextInt(scanner)
		fmt.Fprintln(writer, lzt.Prod(l, r).(*S).a)
	}
}

type S struct {
	a    mint
	size int
}

func (s *S) Op(l, r SInterface) SInterface {
	ll, rr := l.(*S), r.(*S)
	s.a = ll.a.Add(rr.a)
	s.size = ll.size + rr.size
	return s
}
func E() SInterface {
	return &S{a: 0, size: 0}
}
func (s *S) Mapping(l FInterface, r SInterface) SInterface {
	ll, rr := l.(*F), r.(*S)
	s.a = rr.a.Mul(ll.a).Add(mint(rr.size).Mul(ll.b))
	s.size = rr.size
	return s
}

type F struct {
	a, b mint
}

func (f *F) Composition(l, r FInterface) FInterface {
	ll, rr := l.(*F), r.(*F)
	f.a = ll.a.Mul(rr.a)
	f.b = rr.b.Mul(ll.a).Add(ll.b)
	return f
}
func ID() FInterface {
	return &F{a: 1, b: 0}
}

// SInterface Type S interface
type SInterface interface {
	Op(SInterface, SInterface) SInterface
	Mapping(FInterface, SInterface) SInterface
}

// FInterface Type F interface
type FInterface interface {
	Composition(FInterface, FInterface) FInterface
}

// LazySegTree is the data structure for monoids
type LazySegTree struct {
	n, size, log int
	e            func() SInterface
	id           func() FInterface
	d            []SInterface
	lz           []FInterface
}

// NewLazySegTree Constructor
func NewLazySegTree(n int, e func() SInterface, id func() FInterface) *LazySegTree {
	a := make([]SInterface, n)
	for i := 0; i < n; i++ {
		a[i] = e()
	}
	return NewLazySegTreeWithData(a, e, id)
}

// NewLazySegTreeWithData Constructor
func NewLazySegTreeWithData(a []SInterface, e func() SInterface, id func() FInterface) *LazySegTree {
	n := len(a)
	log := 0
	for i := n; i > 0; i >>= 1 {
		log++
	}
	size := 1 << log
	d := make([]SInterface, size<<1)
	lz := make([]FInterface, size)
	for i := 0; i < size<<1; i++ {
		d[i] = e()
	}
	for i := 0; i < size; i++ {
		lz[i] = id()
	}
	for i := 0; i < n; i++ {
		d[i+size] = a[i]
	}
	s := LazySegTree{d: d, lz: lz, n: n, size: size, log: log, e: e, id: id}
	for i := size - 1; i > 0; i-- {
		s.update(i)
	}
	return &s
}

// Set assigns x to a[p]
func (s *LazySegTree) Set(p int, x SInterface) {
	p += s.size
	for i := s.log; i > 0; i-- {
		s.push(p >> i)
	}
	s.d[p] = x
	for i := 1; i <= s.log; i++ {
		s.update(p >> i)
	}
}

// Get returns a[p]
func (s *LazySegTree) Get(p int) SInterface {
	p += s.size
	for i := s.log; i > 0; i-- {
		s.push(p >> i)
	}
	return s.d[p]
}

// Prod returns op(a[l], ..., a[r - 1]), assuming the properties of the monoid. It returns e() if l = r.
func (s *LazySegTree) Prod(l, r int) SInterface {
	if l >= r {
		return s.e()
	}
	sml := s.e()
	smr := s.e()
	l += s.size
	r += s.size
	for i := s.log; i > 0; i-- {
		if (l>>i)<<i != l {
			s.push(l >> i)
		}
		if (r>>i)<<i != r {
			s.push(r >> i)
		}
	}
	for l < r {
		if l&1 == 1 {
			sml.Op(sml, s.d[l])
			l++
		}
		if r&1 == 1 {
			r--
			smr.Op(s.d[r], smr)
		}
		l >>= 1
		r >>= 1
	}
	return sml.Op(sml, smr)
}

// AllProd returns op(a[0], ..., a[n - 1]), assuming the properties of the monoid. It returns e() if n = 0.
func (s *LazySegTree) AllProd() SInterface {
	return s.d[1]
}

// Apply applies a[p] = op_st(a[p], x).
func (s *LazySegTree) Apply(p int, f FInterface) {
	p += s.size
	for i := s.log; i > 0; i-- {
		s.push(p >> i)
	}
	s.d[p].Mapping(f, s.d[p])
	for i := 1; i <= s.log; i++ {
		s.update(p >> i)
	}
}

// ApplyRange applies a[p] = op_st(a[p], x) for all i = l..r-1.
func (s *LazySegTree) ApplyRange(l, r int, f FInterface) {
	if l == r {
		return
	}
	l += s.size
	r += s.size
	for i := s.log; i > 0; i-- {
		if (l>>i)<<i != l {
			s.push(l >> i)
		}
		if (r>>i)<<i != r {
			s.push(r >> i)
		}
	}
	ll := l
	rr := r
	for l < r {
		if l&1 == 1 {
			s.allApply(l, f)
			l++
		}
		if r&1 == 1 {
			r--
			s.allApply(r, f)
		}
		l >>= 1
		r >>= 1
	}
	l = ll
	r = rr
	for i := 1; i <= s.log; i++ {
		if (l>>i)<<i != l {
			s.update(l >> i)
		}
		if (r>>i)<<i != r {
			s.update(r >> i)
		}
	}
}
func (s *LazySegTree) update(k int) {
	s.d[k].Op(s.d[k<<1], s.d[k<<1+1])
}
func (s *LazySegTree) allApply(k int, f FInterface) {
	s.d[k].Mapping(f, s.d[k])
	if k < s.size {
		s.lz[k].Composition(f, s.lz[k])
	}
}
func (s *LazySegTree) push(k int) {
	s.allApply(k<<1, s.lz[k])
	s.allApply(k<<1+1, s.lz[k])
	s.lz[k] = s.id()
}

type mint int64

func (mt mint) Mod() mint {
	m := mint(998244353)
	mt %= m
	if mt < 0 {
		return mt + m
	}
	return mt
}
func (mt mint) Inv() mint {
	return mt.Pow(mint(0).Sub(2))
}
func (mt mint) Pow(n mint) mint {
	p := mint(1)
	for n > 0 {
		if n&1 == 1 {
			p.MulAs(mt)
		}
		mt.MulAs(mt)
		n >>= 1
	}
	return p
}
func (mt mint) Add(x mint) mint {
	return (mt + x).Mod()
}
func (mt mint) Sub(x mint) mint {
	return (mt - x).Mod()
}
func (mt mint) Mul(x mint) mint {
	return (mt * x).Mod()
}
func (mt mint) Div(x mint) mint {
	return mt.Mul(x.Inv())
}
func (mt *mint) AddAs(x mint) *mint {
	*mt = mt.Add(x)
	return mt
}
func (mt *mint) SubAs(x mint) *mint {
	*mt = mt.Sub(x)
	return mt
}
func (mt *mint) MulAs(x mint) *mint {
	*mt = mt.Mul(x)
	return mt
}
func (mt *mint) DivAs(x mint) *mint {
	*mt = mt.Div(x)
	return mt
}