English Class
Time limit2sMemory limit1024 MB
Given K fixed segments in a matching of N left and N right points, count the ways to complete it into a beautiful answer modulo 998244353.
- Level
Medium7 of 10
- Topics
- Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
Bru received a matching problem in English class, in the form shown below.

The matching problem is solved with points on the left and points on the right, arranged vertically. More precisely, you may assume that for every integer with , there is one point at and one point at on the coordinate plane.
Each point has a label next to it. The label next to is a word, and the label next to is its meaning, both in English. Connect two points with a segment when the word and the meaning match. A correct solution makes every point an endpoint of exactly one segment, and every segment has one endpoint with x-coordinate and the other with x-coordinate .
Bru tried hard but found only of the segments. He connected the remaining points so that the result is a correct answer.
While deciding how to connect the remaining points, Bru wanted the final answer to be a beautiful answer.
A correct answer is an answer that satisfies both conditions below.
- Every segment has endpoints and for some (where and are integers).
- All points are endpoints of exactly one segment.
The figures below show correct and incorrect answers.
A beautiful answer is a correct answer that also satisfies both conditions below.
- No three or more segments meet at one point.
- There is no closed region enclosed by the segments. In other words, using only parts of the given segments, you cannot form a polygon with positive area.
The figures below show beautiful and non-beautiful answers.
Bru wants to count the beautiful answers he can build by connecting the remaining points while keeping the segments he has already drawn fixed.
Input
The first line contains two integers and .
Each of the next lines contains two integers and , separated by a space. This means that and are connected by a segment.
Output
Print the number of correct answers that can be made by connecting the remaining points, modulo .
Constraints
- ()
- ()
- ()
- ()




