This page is still under construction.

Parts of this page are still being built. What you see may change.

Kortos

Time limit2sMemory limit1024 MB

Summary
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 NN cards. Each card is described by a suit and a number, and all NN 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 11 and number 44, she may place a card with number 44 (of any suit), or a card of suit 11 whose number is greater than 44; she may not place a card of suit 11 with number 33, nor a card of a different suit whose number is not 44.

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 1 000 000 0071\,000\,000\,007.

Input

The first line contains a single integer NN — the number of cards in hand at the start.

Each of the next NN lines contains two space-separated integers aia_i and bib_i — the suit and the number of the ii-th card.

Output

Output a single integer — the number of different possible final piles, taken modulo 1 000 000 0071\,000\,000\,007.

Constraints

  • 3≤N≤1 000 0003 \le N \le 1\,000\,000
  • 1≤ai,bi≤N1 \le a_i, b_i \le N

Examples1

  1. Example 1

    Input
    4
    1 1
    1 2
    2 2
    2 3
    
    Expected output
    11