Introversion
Time limit3sMemory limit1024 MB
Given a row of 2n slots with some dishes fixed, count the ways to fill the empty slots so each type appears twice and no two equal types are adjacent, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
You run a restaurant called Taste Of Pacific Cuisine (TOPC). This weekend you will host a large banquet for a sizable group of guests. Your chef designed types of dishes. So that every guest can taste each type, you plan to prepare two dishes per type, for a total of dishes at the banquet.
The table is very long and fits exactly dishes in a single line. You plan to set out all dishes at once. No two dishes of the same type may sit next to each other, which gives introverted guests who prefer not to wander around a fair choice of dishes nearby.
Some dishes are already on the table. Count the number of ways to place the remaining dishes so that no two dishes of the same type are adjacent. Call this the intro version of the seating problem. Since the count can be large, output it modulo .
Input
The first line contains an integer , the number of test cases. Each test case starts with an integer . The next line contains integers separated by spaces. If , the -th position on the table is empty. Otherwise, is an integer between and giving the dish type. It is guaranteed that each type appears at most twice in the input. If a type appears twice, the two occurrences are not adjacent.
Output
For each test case, output the number of valid ways to place all the remaining dishes, modulo .
Constraints