Under construction

Still being built. Things may change

Subway Ride

Time limit8sMemory limit1024 MB

Summary
Given N axis-parallel subway segments, find the sum of minimum transfer counts d(i,j) times i times j over all pairs, modulo 1000000007.
Level

Medium7 of 10

Topics
Graph, BFS, Segment tree, Sorting
Solved
No attempts yet

Problem

Jiho is an expert on the subway. Eunho rides the subway often and keeps asking Jiho how to get from one subway line to another. Jiho always gives the route that requires the most transfers, which wears Eunho out.

Fed up with Jiho's answers, Eunho wants to stop asking him. For every pair of subway lines, Eunho will find the minimum number of transfers needed to get from one line to the other.

There are NN subway lines in total. Each line is a segment parallel to the x-axis or the y-axis. If the segments of two subway lines share a point, you can move from one line to the other with one transfer. Two lines that are both parallel to the x-axis, or both parallel to the y-axis, never allow a transfer between them. The length of every line is at least 11.

Help Eunho compute, for every pair of lines, the minimum number of transfers needed to go from one line to the other.

Let d(i,j)d(i,j) be the minimum number of transfers needed to go from line ii to line jj. Find the sum of d(i,j)×i×jd(i,j) \times i \times j over all ordered pairs of integers (i,j)(i,j) with 1≤i,j≤N1 \leq i,j \leq N, modulo 1 000 000 0071\,000\,000\,007. If it is impossible to transfer from line ii to line jj, then d(i,j)=0d(i,j) = 0.

Input

The first line contains the number of subway lines NN.

Each of the next NN lines contains four integers x1x_1, y1y_1, x2x_2, y2y_2, separated by spaces, that describe line ii. Line ii is the segment connecting (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2). The input guarantees that x1=x2x_1 = x_2 or y1=y2y_1 = y_2, and that the length of each segment is at least 11.

Output

Print the answer to the problem on the first line.

Constraints

  • 1≤N≤70001 \leq N \leq 7000
  • For every subway line, −109≤x1,y1,x2,y2≤109-10^9 \leq x_1, y_1, x_2, y_2 \leq 10^9

Examples1

  1. Example 1

    Input
    4
    -1 -2 -1 2
    1 -2 1 2
    2 1 -2 1
    -2 -1 2 -1
    
    Expected output
    98