Moonlit Ribbon Knots
Gats has distinguishable ribbon pieces. Ribbon has endpoint colors and . In its listed orientation, is on the left and is on the right.
Gats prepares one row of ribbons as follows:
- He chooses a permutation of all ribbons uniformly at random.
- Independently for every ribbon, he flips a fair coin. On heads, the ribbon keeps its listed orientation. On tails, its left and right endpoints are exchanged.
The permutation and all coin flips are mutually independent. A coin is still flipped when both endpoints of a ribbon have the same color.
At each of the gaps between consecutive ribbons, Gats makes a neat knot if the two touching endpoint colors are equal. Let be the total number of neat knots.
Compute the expected value of .
Input
The first line contains an integer (), the number of ribbon pieces.
Each of the next lines contains two integers and (), the endpoint colors of ribbon in its listed orientation.
Output
Print the value of modulo .
More precisely, if for integers and with not divisible by , print Such an inverse always exists under the given constraints.
Samples
Sample 1
Input
2 1 1 1 2
Output
499122177
The single gap is neat with probability . Thus , whose required residue is .
Sample 2
Input
4 7 7 7 7 7 7 7 7
Output
9
Every touching endpoint has color , so all gaps are neat. Therefore .