#P1842H. Tenzing and Random Real Numbers
Tenzing and Random Real Numbers
Description
There are uniform random real variables between 0 and 1, inclusive, which are denoted as .
Tenzing has conditions. Each condition has the form of or .
Tenzing wants to know the probability that all the conditions are satisfied, modulo .
Formally, let . It can be shown that the answer can be expressed as an irreducible fraction , where and are integers and . Output the integer equal to . In other words, output the integer that and .
The first line contains two integers and (, ).
Then following lines of input contains three integers , and (, ).
- If , the condition is .
- If , the condition is .
It is guaranteed that all conditions are pairwise distinct.
Output the probability that all the conditions are satisfied, modulo .
Input
The first line contains two integers and (, ).
Then following lines of input contains three integers , and (, ).
- If , the condition is .
- If , the condition is .
It is guaranteed that all conditions are pairwise distinct.
Output
Output the probability that all the conditions are satisfied, modulo .
Note
In the first test case, the conditions are and , and the probability that each condition is satisfied is , so the probability that they are both satisfied is , modulo is equal to .
In the second test case, the answer is .
In the third test case, the answer is .
In the fourth test case, the sum of all variables must equal , so the probability is .