#P15667. [ICPC 2025 Jakarta R] Predisposed

[ICPC 2025 Jakarta R] Predisposed

题目描述

You are given two integers NN and MM. You are also given QQ constraints in the form (Xj,Yj)(X_j, Y_j).

A sequence A=(A1,,AN)A = (A_1, \dots, A_N) of length NN is predisposed\textit{predisposed} if and only if:

  • For each 1iN1 \le i \le N, it holds that 0Ai<M0 \le A_i < M.
  • For each 1jQ1 \le j \le Q, the sum of all AkA_k where kk is a multiple of XjX_j is equal to YjY_j under modulo MM. Formally, XjkAkYj(modM)\sum_{X_j \vert k} A_k \equiv Y_j \pmod{M}.

Find the number of all possible predisposed sequences. As the answer can be quite large, compute it modulo 998  244  353998\;244\;353.

输入格式

The first line contains three integers NN, MM, and QQ (1N,M<998  244  3531 \leq N, M < 998\;244\;353; 1Qmin(N,100  000)1 \leq Q \leq \min(N, 100\;000)).

Each of the next QQ lines contains two integers XjX_j and YjY_j (1XjN1 \leq X_j \leq N; 0Yj<M0 \leq Y_j < M; XpXqX_p \neq X_q for pqp \neq q) representing a constraint.

输出格式

Output an integer in a single line representing the number of predisposed sequences modulo 998  244  353998\;244\;353.

2 3 2
1 0
2 1
1
100000 100000 1
100000 0
373304036

提示

Explanation of Sample 1:\textit{Explanation of Sample 1:} The only predisposed sequence is A=(2,1)A = (2, 1).