Matching In Multiplication

모든 U 정점의 차수가 2인 이분 그래프에서 완전 매칭별 간선 가중치 곱의 합을 998244353으로 나눈 나머지를 구한다.

어려움8그래프DFS수학조합론아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

In the mathematical discipline of graph theory, a bipartite graph is an undirected graph whose vertices can be divided into two disjoint sets UU and VV such that every edge connects some vertex in UU to some vertex in VV. The vertex sets UU and VV are both independent sets, and are usually called the parts of the graph. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.  A matching in a graph is a set of edges without common vertices. A perfect matching is a matching such that each vertex is covered by an edge from the matching.

Little Q misunderstood the definition of bipartite graph. He thinks the size of UU is equal to the size of VV, and for each vertex pp in UU, there are exactly two edges from pp. Based on such weighted graph, he defines the weight of a perfect matching as the product of weights of all the edges included in the matching, and the weight of a graph as the sum of all the perfect matchings' weights.

Your task is to write a program to compute the weight of a weighted graph made by Little Q.

입력

The first line of the input contains an integer nn denoting the size of UU (1n31051 \leq n\leq 3 \cdot 10^5). The vertices in UU and VV are labeled separately by the integers 1,2,,n1, 2, \ldots, n.

In the next nn lines, the ii-th line contains four integers v_i,1v\_{i, 1}, w_i,1w\_{i, 1}, v_i,2v\_{i, 2} and w_i,2w\_{i, 2} which mean that there is an edge between U_iU\_i and V_v_i,1V\_{v\_{i, 1}} with weight w_i,1w\_{i, 1}, and there is another edge between U_iU\_i and V_v_i,2V\_{v\_{i, 2}} with weight w_i,2w\_{i, 2} (1v_i,jn1 \leq v\_{i, j} \leq n, 1w_i,j1091 \leq w\_{i, j} \leq 10^9).

It is guaranteed that the given graph has at least one perfect matching, and there is at most one edge between every pair of vertices.

출력

Print a single line containing a single integer: the weight of the given graph. Since the answer may be very large, print it modulo 998,244,353998\\,244\\,353.