Jiutiao Keli is a girl who keeps her promises, so you will see this problem in this competition. In this problem, the card types and rules are similar but not identical to traditional Dou Dizhu, as this is a story about two landlords. Therefore, we call the card game in this problem "Landlord Fight."
A deck of cards contains A through K of four suits, plus the small joker and big joker, for a total of 54 cards. There is one of each joker, and four of each other rank (corresponding to the four suits). In Landlord Fight, two decks of cards are used. The relative strength of the cards based on their rank is as follows:
$$3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A < 2 < \text{Small Joker} < \text{Big Joker}$$
Suits do not affect the strength of the cards. In each game, a hand consists of $n$ cards. Players can play cards according to specified types, and the first player to empty their hand wins. To simplify the problem, we do not consider suits; that is, all cards of the same rank are considered identical.
In this problem, the allowed card types are (note that this part differs from traditional Dou Dizhu):
| Name | Explanation | Example | Note |
|---|---|---|---|
| Rocket | Both jokers | ♂♀ | |
| Bomb | Four cards of the same rank | 6666 | |
| Single | A single card | 6 | A single joker is also a single |
| Pair | Two cards of the same rank | 66 | Jokers do not form a pair |
| Triple | Three cards of the same rank | 666 | |
| Triple with one | Three cards of the same rank plus one single card of a different rank | 666♂ | A bomb is not a triple with one |
| Triple with two | Three cards of the same rank plus one pair of a different rank | 66699 | |
| Straight | 5 or more consecutive single cards | 3456789 | Cannot contain jokers or 2 |
| Consecutive pairs | 3 or more consecutive pairs | 33445566 | Cannot contain jokers or 2 |
| Triple sequence | 2 or more consecutive triples | 333444555 | Cannot contain jokers or 2 |
| Quad with two | Four cards of the same rank plus two single cards | 444456 / 444455 | Note: cannot take two pairs |
| Airplane (single wings) | Triple sequence plus the same number of distinct single cards | 33344455569J | |
| Airplane (double wings) | Triple sequence plus the same number of distinct pairs | 3334446699 |
Note:
- There is no "consecutive bomb" type, but a hand like 444455556666 can still be played; it is treated as a 444555666 triple sequence with 456 as single wings.
- The big joker and small joker have different ranks, so an airplane with jokers is legal, e.g., 333444♂♀.
- It is easy to verify that the rules for the above types are well-defined, meaning any legal set of cards has a unique type.
Two hands belong to the same type if and only if their names are the same and they contain the same number of cards. There is a hierarchy among hands of the same type (the Rocket is unique and does not need to be compared):
- The strength of a "triple with one" or "triple with two" depends on the rank of the three identical cards.
- The strength of an airplane depends on the strength of the triple sequence.
- The strength of a "quad with two" depends on the rank of the four identical cards.
- The strength of other types depends on the highest-ranking card in the set.
Card set $A$ can be played after card set $B$ if and only if at least one of the following three conditions is met:
- $B$ and $A$ are of the same type, and $B$ is strictly stronger than $A$.
- $A$ is not a bomb, but $B$ is a bomb.
- $A$ is not a rocket, but $B$ is a rocket.
The following is a description of the Landlord Fight game process (note that this part differs significantly from traditional Dou Dizhu):
- In Landlord Fight, there are three players: one questioner and two landlords. The game requires two decks of cards. Each of the two landlords has 20 cards in their hand; the first landlord's hand comes from the first deck, and the second landlord's hand comes from the second deck.
- The questioner can ask the landlords several questions. Each time, the questioner names a card set $A$ of any type and any strength. The landlords must answer whether there exists a subset of their hand that can be played after $A$. If it exists, they answer $1$; otherwise, they answer $0$.
- If the two landlords give different answers, the landlord who answered $1$ wins.
- If the two landlords give the same answer, the questioner continues to ask.
Note that after answering, the landlords do not actually play their cards; each landlord's hand remains unchanged throughout the questioning process.
For example, if the first landlord's hand is 555666777JJJQQQAAA33 and the second landlord's hand is 33445566778899AAA222, a possible game process is:
- The questioner asks ♂♀, both landlords answer 0.
- The questioner asks 3333, both landlords answer 0.
- The questioner asks 3334, both landlords answer 1.
- The questioner asks QQQKKK♂♀, both landlords answer 0.
- The questioner asks 34567, the first landlord answers 0, the second landlord answers 1, and the second landlord wins.
It is easy to see that in some cases, no matter how the questioner asks, the two landlords' answers are always identical. One example is when the two landlords' hands are identical (though there are other cases). We say that in such cases, the two landlords have reached a draw.
Given two sets of cards, the first set has $n$ cards ($0 \leq n \leq 20$) and the second set has $m$ cards ($0 \leq m \leq 20$). How many initial situations $(A, B)$ are there (where $A$ is the first landlord's hand and $B$ is the second landlord's hand) such that:
- $A$ contains the first set of $n$ cards.
- $B$ contains the second set of $m$ cards.
- $A$ and $B$ result in a draw.
Input
The first line contains an integer $n$ ($0 \leq n \leq 20$), the number of cards in the first set. The next $n$ integers describe the cards in the first landlord's hand.
The second line contains an integer $m$ ($0 \leq m \leq 20$), the number of cards in the second set. The next $m$ integers describe the cards in the second landlord's hand.
Specifically, we use $1$ to represent rank A, $11$ for J, $12$ for Q, $13$ for K, $14$ for the small joker, and $15$ for the big joker. It is guaranteed that the input is legal, meaning that in either set, the number of cards of any rank does not exceed the number of cards of that rank in a single deck.
Output
Output a single integer representing the number of initial situations $(A, B)$ that satisfy the conditions. The answer may be large, so output it modulo $998244353$.
Note that in this problem, we do not consider suits. If two situations have the same composition of ranks, they are considered the same, even if the suits differ.
Examples
Input 1
20 5 5 5 6 6 6 7 7 7 11 11 11 12 12 12 1 1 1 3 3 20 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 1 1 2 2 2
Output 1
0
Input 2
20 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 1 1 2 2 2 20 3 3 4 4 5 5 6 6 7 7 8 8 9 9 11 11 11 2 2 2
Output 2
1
Input 3
4 3 4 5 6 4 7 8 9 10
Output 3
787215587
Subtasks
- Subtask 1 (13 points): $n, m \geq 18$
- Subtask 2 (25 points): $n = 20$
- Subtask 3 (10 points): $n = m = 0$
- Subtask 4 (29 points): $n, m \geq 10$
- Subtask 5 (23 points): $0 \leq n, m \leq 20$