Under construction

Still being built. Things may change

∏i=1N(Ri−Li+1)\prod_{i=1}^N(R_i-L_i+1) Trees

Time limit2sMemory limit1024 MB

Summary
Given a rooted tree with per-vertex caps and subtree sum lower bounds, sum the minimum weighted cost over all cost vectors in given ranges, modulo 998244353.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Combinatorics, Math
Solved
No attempts yet

Problem

A tree with NN vertices rooted at vertex 1 is given. Assign a nonnegative integer aia_i to each vertex ii.

The values aia_i must satisfy the following two conditions for every 1≤i≤N1 \le i \le N.

  • ai≤pia_i \le p_i
  • Let SiS_i be the sum of aja_j over all vertices jj in the subtree of vertex ii. Then Si≥qiS_i \ge q_i.

For each (a1,a2,…,aN)(a_1, a_2, \ldots, a_N) that satisfies these conditions, let f(c1,c2,…,cN)f(c_1, c_2, \ldots, c_N) be the minimum value of ∑i=1Nciai\sum_{i=1}^N c_i a_i.

Find the following value modulo 998 244 353998\,244\,353.

∑c1=L1R1∑c2=L2R2⋯∑cN=LNRNf(c1,c2,⋯ ,cN)\sum_{c_1=L_1}^{R_1}\sum_{c_2=L_2}^{R_2}\cdots\sum_{c_N=L_N}^{R_N}f(c_1,c_2,\cdots,c_N)

Input

The first line contains NN, the number of vertices (1≤N≤2501 \le N \le 250).

Each of the next N−1N-1 lines contains two integers sis_i and eie_i separated by a space, meaning there is an edge between sis_i and eie_i. (1≤si,ei≤N1 \le s_i, e_i \le N)

For each 1≤i≤N1 \le i \le N, the (N+i)(N+i)-th line contains pip_i, qiq_i, LiL_i, and RiR_i separated by spaces. (1≤Li≤Ri≤2501 \le L_i \le R_i \le 250; 0≤pi,qi≤2500 \le p_i, q_i \le 250; ∑i=1Npi≤250\sum_{i=1}^N p_i \le 250)

The given graph is a tree, and only inputs where a valid (a1,a2,…,aN)(a_1, a_2, \ldots, a_N) exists are given.

Output

Print the value from the statement modulo 998 244 353998\,244\,353. Note that 998 244 353=119×223+1998\,244\,353=119\times 2^{23}+1 is prime.

Hint

Vertex jj is in the subtree of vertex ii if and only if j=ij=i or vertex ii is an ancestor of vertex jj.

Examples2

  1. Example 1

    Input
    4
    1 2
    1 3
    1 4
    2 5 5 5
    1 1 2 2
    2 1 3 3
    1 1 1 1
    
    Expected output
    14
    
  2. Example 2

    Input
    6
    1 2
    1 3
    2 4
    2 5
    4 6
    1 7 1 3
    3 2 2 4
    2 1 1 4
    4 2 4 6
    2 0 1 5
    2 0 2 5
    
    Expected output
    39072