Under construction

Still being built. Things may change

Functions and Minimum Spanning Trees

Time limit3sMemory limit1024 MB

Summary
Given edges with weights at^2+b_i t+c_i (a shared), find the integral over [t1, t2] of the MST weight sum, modulo 1e9+7.
Level

Medium7 of 10

Topics
Minimum spanning tree, Union-find, Math
Solved
No attempts yet

Problem

A simple connected graph with VV vertices numbered from 11 to VV and EE edges is given.

The weight of each edge is a quadratic function at2+bit+ciat^2+b_it+c_i that changes with time tt. The coefficient aa is the same for every edge.

Define f(t)f(t) as the sum of the weights of the minimum spanning tree at time tt.

Given integers t1t_1 and t2t_2, find the value of ∫t1t2f(t)dt\int_{t_1}^{t_2}f(t)dt.

A minimum spanning tree is a tree, among the subgraphs that connect all vertices of the given graph, whose total weight is minimal.

Input

The first line contains integers VV, EE, and aa, separated by spaces. (1≤V≤1001 \le V \le 100, 1≤E≤2501 \le E \le 250, −1 000≤a≤1 000-1\,000 \le a \le 1\,000, a≠0a \neq 0)

Each of the next EE lines contains four integers XX, YY, bib_i, and cic_i, separated by spaces. (1≤X,Y≤V1 \le X, Y \le V, −1 000≤bi,ci≤1 000-1\,000 \le b_i, c_i \le 1\,000)

This means the weight of the edge connecting vertex XX and vertex YY is at2+bit+ciat^2+b_it+c_i.

The last line contains integers t1t_1 and t2t_2, separated by a space. (−1 000≤t1≤t2≤1 000-1\,000 \le t_1 \le t_2 \le 1\,000)

Output

Print m×n−1 mod (109+7)m \times n^{-1} \bmod (10^9+7), where the value of the integral is the reduced fraction mn\frac{m}{n} with integer mm and positive integer nn. n−1n^{-1} is the multiplicative inverse of nn modulo 109+710^9+7.

It can be proven that the answer can be written in this form for every nn that is not a multiple of 109+710^9+7.

Hint

If needed, you may use (ab−1)+(cd−1)≡(ad+bc)×(bd)−1(mod109+7)(ab^{-1})+(cd^{-1}) \equiv (ad+bc)\times(bd)^{-1} \pmod {10^9+7}.

Examples2

  1. Example 1

    Input
    2 1 3
    2 1 1 4
    -3 7
    
    Expected output
    430
    
  2. Example 2

    Input
    3 3 3
    1 2 2 -3
    3 1 1 6
    2 3 -4 12
    -1 10
    
    Expected output
    800001995