PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000225. Thủ thư không chăm chỉ (Lazy Librarian)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Thủ thư không chăm chỉ (Lazy Librarian)

Lazy Librarian

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn books arranged in a row, whose current positions are numbered from 11 to nn from left to right. Each book has its own correct position in the sorted order and its page count. The correct positions form a permutation of 1,2,…,n1,2,\ldots,n.

A pair of books at current positions i<ji<j is out of order if the correct position of the book at ii is greater than that of the book at jj. Such a pair incurs discomfort equal to the sum of the two books' page counts. Total discomfort is the sum of these contributions over all out-of-order pairs.

On each of mm consecutive days, two books at current positions x,yx,y exchange places. If x=yx=y, nothing changes. Each book retains both of its attributes when moved.

After every swap, print the total discomfort of the resulting arrangement modulo 109+710^9+7. No automatic sorting occurs between days.

Input

  • The first line contains integers n,mn,m, the number of books and swap days.
  • Each of the next nn lines contains integers ai,via_i,v_i, the correct position and page count of the book initially at current position ii. All values aia_i are distinct.
  • Each of the next mm lines contains integers xj,yjx_j,y_j, the two current positions whose books are swapped on day jj.

Output

Print exactly mm lines. Line jj contains the total discomfort after swap jj, modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤50001\le n,m\le 5000; aa is a permutation of 1,…,n1,\ldots,n; 1≤vi≤1051\le v_i\le10^5; 1≤xj,yj≤n1\le x_j,y_j\le n.
  • Subtask 2 (30 points): 1≤n,m≤200001\le n,m\le 20000; aa is a permutation of 1,…,n1,\ldots,n; 1≤vi≤1051\le v_i\le10^5; 1≤xj,yj≤n1\le x_j,y_j\le n.
  • Subtask 3 (50 points): 1≤n,m≤5⋅1041\le n,m\le5\cdot10^4; aa is a permutation of 1,…,n1,\ldots,n; 1≤vi≤1051\le v_i\le10^5; 1≤xj,yj≤n1\le x_j,y_j\le n.

Examples

Input

3 3
2 5
1 3
3 2
1 2
2 3
1 3

Output

0
7
13

Explanation

Initially the books, recorded as (correct position, page count), are (2,5),(1,3),(3,2)(2,5),(1,3),(3,2). The initial inversion contributes 5+3=85+3=8. After swapping positions 1,21,2, the books become (1,3),(2,5),(3,2)(1,3),(2,5),(3,2), with no inversion, so the first output is 00. Next, swapping positions 2,32,3 produces (1,3),(3,2),(2,5)(1,3),(3,2),(2,5), whose only inversion has weight 2+5=72+5=7. Finally, swapping positions 1,31,3 produces (2,5),(3,2),(1,3)(2,5),(3,2),(1,3). The inversions between positions (1,3)(1,3) and (2,3)(2,3) contribute 5+3=85+3=8 and 2+3=52+3=5 respectively, totaling 1313. Thus the outputs are 0,7,130,7,13.