NOI Contest Archive

Archive / 2025 / Final Round — Day 1

duck-pond

Medium · National Olympiad in Informatics 2025 day 1

You are given a pond that is a grid of size \(h \times w\). Each cell in the grid can either contain a duck or be empty.

There are \(h\) kids standing along the rows (one per row) and \(w\) kids standing along the columns (one per column). Each kid is standing at the edge of their respective row or column and can only see consecutive ducks starting from the edge they are facing.

Given these observations, how many valid configurations of ducks are there on the grid? (Answer can be zero if the given data are impossible) Since the answer can be large, output the answer modulo \(1e9+7\)

Sample grid:

image

Input Format

• An integer \(h\) — number of rows. • An integer \(w\) — number of columns. • A list of \(h\) integers \(r[1 \dots h]\) — number of consecutive ducks from the left in each row. • A list of \(w\) integers \(c[1 \dots w]\) — number of consecutive ducks from the top in each column.

Output Format

• A single integer — the number of valid configurations of ducks that satisfy the given row and column constraints modulo \(1e9+7\).

Constraints

Subtasks

Subtask 1 - \(10\%\)

Subtask 2 - \(20\%\)

Subtask 3 - \(30\%\)

Subtask 4 - \(40\%\)

Sample Input 0

3 4
0 3 1
0 2 3 0

Sample Output 0

2