QOJ.ac

QOJ

Limite de temps : 2 s Limite de mémoire : 512 MB Points totaux : 100 Hackable ✓

#4889. Stupid Online Judge

Statistiques

You have discovered that SOJ, or Stupid Online Judge, has become increasingly stupid. For instance, SOJ cannot cook, while your dog can run into the kitchen and prepare a four-course meal with soup.

After some exploration, you discovered that the reason for SOJ's stupidity is its built-in online linear algebra solver! The administrators, obsessed with linear algebra, spend their days researching how to quickly calculate $2048$-order determinants, leaving SOJ unmaintained and consuming a massive amount of its performance.

This system has a built-in program capable of solving the determinant of a matrix of order $114514$ with rank $114513$ in under a second. Out of sheer boredom, you decide to feed this system several determinants, but you do not know when it will become overwhelmed. To better determine if the system has been broken, you decide to calculate the answers to the problems you provide yourself.

Of course, the administrators believe their own intelligence is online, so they wrote a set of rules to prevent the system from being broken. Consequently, you must upload the determinant in the following format:

  • You are given a rooted tree with $n$ nodes and a weight $v_i$ for each node, with node $1$ as the root. You also provide the system with an array $A$ of length $k$. Construct a square matrix $B$ of order $k$ where $b_{i,j} = v_{\mathrm{LCA}(A_i, A_j)}$, that is:

$$ B = \begin{bmatrix} v_{\mathrm{LCA}(A_1, A_1)} & v_{\mathrm{LCA}(A_1, A_2)} & \cdots & v_{\mathrm{LCA}(A_1, A_k)} \\ v_{\mathrm{LCA}(A_2, A_1)} & v_{\mathrm{LCA}(A_2, A_2)} & \cdots & v_{\mathrm{LCA}(A_2, A_k)} \\ \vdots & \vdots & \ddots & \vdots \\ v_{\mathrm{LCA}(A_k, A_1)} & v_{\mathrm{LCA}(A_k, A_2)} & \cdots & v_{\mathrm{LCA}(A_k, A_k)} \\ \end{bmatrix} $$

  • Here, $\mathrm{LCA}(x, y)$ is the lowest common ancestor of nodes $x$ and $y$. Once you provide the array $A$, the determinant of the constructed matrix will be calculated.

With such a set of rules, SOJ seems even stupider. Fearing that excessively large numbers might scare the inexperienced administrators, you decide to take the value of the matrix modulo $998244353$ before showing it to them.

Input

The first line contains two positive integers $n$ and $k$, representing the number of nodes in the tree and the length of $A$, respectively.

The second line contains $n$ non-negative integers $v_i$, representing the weight of each node.

The third line contains $k$ positive integers $A_i$, representing the array provided to the system.

The next $n - 1$ lines each contain two positive integers $u, v$, representing an undirected edge $(u, v)$ in the tree.

It is guaranteed that the given edges form a tree.

Output

A single non-negative integer in the range $[0, 998244353)$ representing the answer.

Examples

Input 1

3 2
1 2 3
2 3
1 2
1 3

Output 1

5

Note 1

The determinant of the resulting matrix is:

$$ \begin{vmatrix} 2 & 1 \\ 1 & 3 \end{vmatrix} = 2 \times 3 - 1 \times 1 = 5 $$

Input 2

5 1
225348648 810032443 884606707 501975769 428153443
4
1 5
3 5
2 1
4 1

Output 2

501975769

Input 3

10 5
948691377 65381930 199744893 359204892 47703053 527403959 682504024 581643492 374119650 567695458
5 7 3 8 2
6 3
8 6
10 3
9 3
2 6
1 2
5 3
7 9
4 1

Output 3

141670859

Input 4

See ex_online4.in and ex_online4.ans in the provided files. This example satisfies the properties of Subtask 3.

Input 5

See ex_online5.in and ex_online5.ans in the provided files. This example satisfies the properties of Subtask 4.

Input 6

See ex_online6.in and ex_online6.ans in the provided files. This example satisfies the properties of Subtask 6.

Constraints

For all data, $1 \leq n, k \leq 5 \times 10^5$, $v_i \in [0, 998244353)$, $A_i \in [1, n]$.

Subtask ID Score Dependencies Special Conditions
$1$ $3$ None $k > n$
$2$ $6$ $n \leq 10$
$3$ $11$ $k \leq 600$
$4$ $29$ $2$ $n \leq 3000$
$5$ $16$ None $A$ is a permutation of $1 \dots n$
$6$ $35$ $1,3,4,5$ None

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.