#P6846. [CEOI 2019] Amusement Park

[CEOI 2019] Amusement Park

Description

There is a directed graph with nn vertices and mm edges. The graph has no multiple edges and no self-loops, and there is no cycle between any two vertices.

Now we want to reverse the direction of some edges so that the directed graph becomes acyclic.

You need to compute the answer after taking mod 998244353\bmod\ 998244353: for every valid way of reversing edges that makes the graph acyclic, take the number of edges that are reversed in this way; sum these numbers over all valid ways.

Input Format

The first line contains two integers n,mn, m.

The next mm lines each contain two integers ai,bia_i, b_i, meaning there is a directed edge that starts at aia_i and ends at bib_i.

Output Format

Print one integer: the sum, over every way of reversing edges that makes the graph acyclic, of the number of edges that must be reversed, taken mod 998244353\bmod\ 998244353.

2 1
1 2

1

3 3
1 2
2 3
1 3
9

Hint

Sample Explanation

Sample 1 Explanation

There are two possible ways:

  • Reverse the direction.
  • Do not reverse the direction.

So the output is 1+0=11+0=1.

Sample 2 Explanation

There are six feasible ways:

  • 12,23,131\to2,2\to3,1\to3
  • 12,32,131\to2,3\to2,1\to3
  • 12,32,311\to2,3\to2,3\to1
  • 21,23,132\to1,2\to3,1\to3
  • 21,23,312\to1,2\to3,3\to1
  • 21,32,312\to1,3\to2,3\to1

So the output is 0+1+2+1+2+3=90+1+2+1+2+3=9.

Constraints

For 100%100\% of the testdata, it is guaranteed that 1n181\le n\le 18, 0mn×(n1)20\le m\le \frac{n\times (n-1)}{2}, 1ai,bin1\le a_i,b_i\le n, aibia_i\not=b_i, for iji\not=j we have aiaja_i\not=a_j or bibjb_i\not=b_j, and the unordered pairs {ai,bi}\{a_i,b_i\} are all distinct.

The detailed subtask constraints and scores are as follows:

Subtask ID Constraint Score
1 n3n\le 3 77
2 n6n\le 6 1212
3 n10n\le 10 2323
4 n15n\le 15 2121
5 No additional constraints 3737

Note

This problem is translated from Central-European Olympiad in Informatics 2019 Day 2 T1 Amusement Park

Translated by ChatGPT 5