QOJ.ac

QOJ

時間限制: 1.0 s 記憶體限制: 512 MB 總分: 100 可 Hack ✓

#12811. 波浪序列

统计

你见过波浪吗?那是大自然中奇妙的景象。小 Q 对这种奇妙的事物非常着迷,他甚至喜欢所有看起来像波浪的东西。形式化地,他称一个序列 $a_1, a_2, \dots, a_n$ 为“波浪序列”(wavel),当且仅当 $a_1 < a_2 > a_3 < a_4 > a_5 < a_6 \dots$。

现在,给定两个序列 $a_1, a_2, \dots, a_n$ 和 $b_1, b_2, \dots, b_m$,小 Q 想要找到两组序列 $f_1, f_2, \dots, f_k$ 和 $g_1, g_2, \dots, g_k$(其中 $1 \le f_i \le n$,$f_i < f_{i+1}$ 且 $1 \le g_i \le m$,$g_i < g_{i+1}$),使得对于所有 $i$ 都有 $a_{f_i} = b_{g_i}$,并且序列 $a_{f_1}, a_{f_2}, \dots, a_{f_k}$ 是一个波浪序列。此外,小 Q 想知道存在多少对这样的序列 $f$ 和 $g$。请编写一个程序来帮助他计算答案。

输入格式

第一行包含两个整数 $n$ 和 $m$,分别表示序列 $a$ 和 $b$ 的长度($1 \le n, m \le 2000$)。

第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($1 \le a_i \le 2000$)。

第三行包含 $m$ 个整数 $b_1, b_2, \dots, b_m$($1 \le b_i \le 2000$)。

输出格式

输出一行,包含一个整数,即问题的答案。由于答案可能非常大,请输出其对 $998\,244\,353$ 取模的结果。

样例

输入 1

3 5
1 5 3
4 1 1 5 3

输出 1

10

说明

以下是所有符合条件的序列对:

(1) $f = (1), g = (2)$。 (2) $f = (1), g = (3)$。 (3) $f = (2), g = (4)$。 (4) $f = (3), g = (5)$。 (5) $f = (1, 2), g = (2, 4)$。 (6) $f = (1, 2), g = (3, 4)$。 (7) $f = (1, 3), g = (2, 5)$。 (8) $f = (1, 3), g = (3, 5)$。 (9) $f = (1, 2, 3), g = (2, 4, 5)$。 (10) $f = (1, 2, 3), g = (3, 4, 5)$。

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.