Problem: An ant moves on the real line with constant speed of unit per second. It starts at and always moves to the right (so its position increases by each second).
There are portals, the -th of which is located at position and teleports to position . Each portal can be either active or inactive. The initial state of the -th portal is determined by : if then the -th portal is initially inactive, if then the -th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal):
- if the portal is inactive, it becomes active (in this case the path of the ant is not affected);
- if the portal is active, it becomes inactive and the ant is instantly teleported to the position , where it keeps on moving as normal.
How long (from the instant it starts moving) does it take for the ant to reach the position ? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo .
Input Format: The first line contains the integer () — the number of portals.
The -th of the next lines contains three integers , and (, ) — the position of the -th portal, the position where the ant is teleported when it travels through the -th portal (if it is active), and the initial state of the -th portal.
The positions of the portals are strictly increasing, that is . It is guaranteed that the integers are all distinct.
Output Format: Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position . Since the answer may be very large, output it modulo .
Note: Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed and the time spent during the movement is written above the arrow). Notice that the total time is .
Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed and the time spent during the movement is written above the arrow). Notice that the total time is .
Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from to .