Ticket Swapping

Passengers riding one direction on a line pay a decreasing per-stop fare and may swap entry cards where trips overlap, so compute the maximum total fare loss.

Hard8GreedySortingPrefix sumMathNo attempts yetTime limit5sMemory limit512 MB

Problem

The city opened its first subway line. It has NN stations, numbered 1 to NN, and fares are charged through entry cards instead of tickets.

A passenger entering the subway receives one entry card, which records the station where the passenger entered. On the way out the passenger has to give up an entry card, and pays according to the distance between the station printed on the card and the station where the card is surrendered.

  • If the two stations are the same, the passenger pays nothing.
  • If they are adjacent, the passenger pays NN pounds.
  • If the distance is two stations, the passenger pays 2N12N - 1: a charge of NN for the first stop and N1N - 1 for the second.
  • The third stop costs N2N - 2, so a three station trip costs 3N33N - 3. The fourth stop costs N3N - 3, and the ii-th stop costs N+1iN + 1 - i.
  • Travelling from one end of the line to the other (a distance of N1N - 1 stations) therefore costs 2 pounds for the last station and (N2+N2)/2(N^2 + N - 2) / 2 in total.

After introducing this system the city noticed that its revenue was smaller than expected. The cause was passengers swapping entry cards. Suppose one person enters at station AA, travels two stations and leaves at station BB, while another person enters at station BB, travels three stations and leaves at station CC. Normally the two pay 2N1+3N3=5N42N - 1 + 3N - 3 = 5N - 4 in total. If they swap their cards at station BB, the first person surrenders a card printed with BB at station BB, registers a distance of zero and travels for free. The second person surrenders a card printed with AA at station CC, a distance of 5 stations, and pays 5N105N - 10. The city loses six pounds.

The city wants to know how much it can lose if this practice becomes widespread. Consider only one direction of the line (from station 1 to station NN, passing through every station in order) and only one train. A passenger travelling from oo to ee obtains a card at oo, can swap that card any number of times with any other passenger anywhere between oo and ee, including passengers who leave at oo and passengers who enter at ee, and then leaves at ee after surrendering some card. Surrendering a card is required in order to leave. A passenger never leaves the train in the middle, so nobody surrenders the card being held and obtains a new one.

You are given a map of traffic that says how many passengers travel from which station to which station. Compute the loss of the city when passengers swap their cards so that this loss is as large as possible. The loss is the total fare when nobody swaps a card, minus the smallest total fare that swapping can produce.

Input

The first line of the input contains the number of test cases TT. The first line of each test case contains the number of stations NN and the number MM of origin and endpoint pairs. Stations are numbered 1 to NN. Each of the next MM lines contains three integers oio_i, eie_i and pip_i, meaning that pip_i passengers enter at station oio_i and leave at station eie_i.

Limits:

  • 1T201 \le T \le 20
  • 2N1092 \le N \le 10^9
  • 1M10001 \le M \le 1000
  • 1oi<eiN1 \le o_i < e_i \le N
  • 1pi1091 \le p_i \le 10^9

Output

For each test case, print one line in the format Case #x: y, where xx is the test case number starting from 1, and yy is the loss of the city caused by card swapping, modulo 1000002013.

Hint

The first example case is the situation described in the statement: the two passengers meet at station 3 and swap their cards. In the second example case the two passengers never share a stretch of the line, so they cannot swap and the city loses nothing. In the third example case only one of the two earlier passengers can swap cards with the later passenger.