#abc321g. G - Electric Circuit

G - Electric Circuit

Score : 600600 points

问题描述

你正在使用 NN 个编号为 11NN 的部件和 MM 根电缆创建一个电路。这些部件共有 MM 个红色接头和 MM 个蓝色接头,其中第 ii 个红色接头连接到部件 RiR_i,第 ii 个蓝色接头连接到部件 BiB_i。每根电缆连接一个红色接头和一个蓝色接头,特别地,允许将同一个部件上的两个接头连接起来。不允许在一个接头上连接两根或更多的电缆。因此,总共有 M!M! 种不同的方式来连接这 MM 根电缆(不区分电缆之间)。

ss 表示当把这个电路看作是一个由部件作为顶点、电缆作为边的图时的连通分量数量。在随机从 M!M! 种方式中选择一种来连接 MM 根电缆时,求 ss 的期望值对 998244353998244353 取模的结果。

寻找一个对 998244353998244353 取模后的期望值是什么意思?可以证明所求的期望值始终是一个有理数。此外,在本题的约束条件下,可以证明当该值表示为两个互质整数 PPQQ 的形式 PQ\frac{P}{Q} 时,存在恰好一个整数 RR 满足 R×QP(mod998244353)R \times Q \equiv P\pmod{998244353} 以及 0R<9982443530 \leq R < 998244353。找出这个 RR

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are creating an electrical circuit using NN parts numbered 11 to NN and MM cables. These parts have a total of MM red terminals and MM blue terminals, with the ii-th red terminal attached to part RiR_i and the ii-th blue terminal attached to part BiB_i. Each cable connects one red terminal and one blue terminal. In particular, it is allowed to connect two terminals attached to the same part. You cannot connect two or more cables to a terminal. Therefore, there are M!M! ways in total to connect the MM cables (the cables are not distinguished from each other).

Let ss be the number of connected components when seeing this circuit as a graph with parts as vertices and cables as edges. Find the expected value, modulo 998244353998244353, of ss when randomly choosing the way to connect the MM cables out of the M!M! ways.

What does it mean to find an expected value modulo 998244353998244353? It can be proved that the sought expected value is always a rational number. Also, under the constraints of this problem, it can be proved that when this value is expressed as PQ\frac{P}{Q} using two coprime integers PP and QQ, there is exactly one integer RR that satisfies R×QP(mod998244353)R \times Q \equiv P\pmod{998244353} and 0R<9982443530 \leq R \lt 998244353. Find this RR.

Constraints

  • 1N171\leq N \leq 17
  • 1M1051 \leq M \leq 10^5
  • 1Ri,BiN1 \leq R_i, B_i \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

R1R_1 R2R_2 \dots RMR_M

B1B_1 B2B_2 \dots BMB_M

Output

Print the expected value of ss, modulo 998244353998244353.

Sample Input 1

3 2
1 2
3 2

Sample Output 1

499122178

Let (i,j)(i, j) denote the action of connecting the ii-th red terminal and the jj-th blue terminal with a cable.

  • For (1,1),(2,2)(1,1), (2,2): There will be two connected components, {1,3}\lbrace 1,3 \rbrace and {2}\lbrace 2 \rbrace, so s=2s=2.
  • For (1,2),(2,1)(1,2), (2,1): The whole graph constitutes one connected component, so s=1s=1.

Thus, the expected value of ss is 32499122178(mod998244353)\frac{3}{2} \equiv 499122178 \pmod {998244353}.

Sample Input 2

17 5
1 1 1 1 1
1 1 1 1 1

Sample Output 2

17

s=Ns=N no matter how you connect the cables.

Sample Input 3

8 10
2 4 7 1 7 6 1 4 8 1
5 1 5 2 5 8 4 6 1 3

Sample Output 3

608849831

update @ 2024/3/10 01:41:36