Kortos
Time limit2sMemory limit1024 MB
Count the distinct ordered piles a player can build from N distinct cards where each new card matches the top card's number, or matches its suit with a larger number, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Graph, Sorting
- Solved
- No attempts yet
Problem
A player is playing a card game. At the start she holds cards. Each card is described by a suit and a number, and all cards are distinct.
She builds a single pile. First she places any one card of her choice. After that, on each turn she may take a card still in her hand and place it on top of the current top card, provided the new card is either:
- the same number as the current top card, or
- the same suit as the current top card and has a strictly greater number.
For example, if the current top card has suit and number , she may place a card with number (of any suit), or a card of suit whose number is greater than ; she may not place a card of suit with number , nor a card of a different suit whose number is not .
After placing the first card she may stop at any time.
Count how many different final piles are possible. Two piles are different if the cards they contain differ, or the cards are the same but their order differs. Because the answer can be large, output it modulo .
Input
The first line contains a single integer — the number of cards in hand at the start.
Each of the next lines contains two space-separated integers and — the suit and the number of the -th card.
Output
Output a single integer — the number of different possible final piles, taken modulo .