atcoder#ABC255H. [ABC255Ex] Range Harvest Query

[ABC255Ex] Range Harvest Query

Score : 600600 points

Problem Statement

There are NN trees. On Day 00, each tree bears no fruits. On the morning of Day 11 and subsequent days, the ii-th tree bears ii new fruits for each i=1,2,,Ni = 1, 2, \ldots, N.

Takahashi will perform QQ harvesting. For each i=1,2,,Qi = 1, 2, \ldots, Q, the ii-th harvesting takes place on the night of Day DiD_i, collecting all fruits remaining on the LiL_i-th through RiR_i-th trees at that point.

For each of the QQ harvesting, print the number of fruits Takahashi will collect, modulo 998244353998244353.

Constraints

  • 1N10181 \leq N \leq 10^{18}
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1D1<D2<<DQ10181 \leq D_1 \lt D_2 \lt \cdots \lt D_Q \leq 10^{18}
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN QQ

D1D_1 L1L_1 R1R_1

D2D_2 L2L_2 R2R_2

\vdots

DQD_Q LQL_Q RQR_Q

Output

Print QQ lines. For each i=1,2,,Qi = 1, 2, \ldots, Q, the ii-th line should contain the number of fruits Takahashi will collect in the ii-th harvesting, modulo 998244353998244353.

5 3
2 2 3
3 3 4
5 1 5
10
15
50

For each i=1,2,3,4,5i = 1, 2, 3, 4, 5, let AiA_i be the number of fruits remaining on the ii-th tree. Now, we use the sequence A=(A1,A2,A3,A4,A5)A = (A_1, A_2, A_3, A_4, A_5) to represent the numbers of fruits remaining in the trees.

  • On Day 00, we have A=(0,0,0,0,0)A = (0, 0, 0, 0, 0).
  • On the morning of Day 11, each tree bears new fruits, and we have A=(1,2,3,4,5)A = (1, 2, 3, 4, 5).
  • On the morning of Day 22, each tree bears new fruits, and we have A=(2,4,6,8,10)A = (2, 4, 6, 8, 10).
  • On the night of Day 22, Takahashi performs the 11-st harvesting. 4+6=104 + 6 = 10 fruits are collected, and we have A=(2,0,0,8,10)A = (2, 0, 0, 8, 10).
  • On the morning of Day 33, each tree bears new fruits, and we have A=(3,2,3,12,15)A = (3, 2, 3, 12, 15).
  • On the night of Day 33, Takahashi performs the 22-nd harvesting. 3+12=153 + 12 = 15 fruits are collected, and we have A=(3,2,0,0,15)A = (3, 2, 0, 0, 15).
  • On the morning of Day 44, each tree bears new fruits, and we have A=(4,4,3,4,20)A = (4, 4, 3, 4, 20).
  • On the morning of Day 55, each tree bears new fruits, and we have A=(5,6,6,8,25)A = (5, 6, 6, 8, 25).
  • On the night of Day 55, Takahashi performs the 33-rd harvesting. 5+6+6+8+25=505 + 6 + 6 + 8 + 25 = 50 fruits are collected, and we have A=(0,0,0,0,0)A = (0, 0, 0, 0, 0).
711741968710511029 1
82803157126515475 516874290286751784 588060532191410838
603657470

Be sure to print the numbers modulo 998244353998244353.