Fish

Time limit3sMemory limit128 MB

Summary
Given fish lengths and gem kinds, count how many distinct gem-count combinations a single fish can ever hold, modulo M, where a fish can eat another only if at least twice as long.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Combinatorics, Tree
Solved
No attempts yet

Problem

Far away, in the middle of a desert, there is a lake. Originally the lake held FF fish. Among the most valuable gemstones on Earth, KK different kinds were chosen, and each of the FF fish was made to swallow exactly one gem. Because KK may be smaller than FF, several fish may have swallowed gems of the same kind.

As time went by, some fish ate others. One fish can eat another if and only if it is at least twice as long: fish AA can eat fish BB exactly when LA≥2⋅LBL_A \ge 2 \cdot L_B. There is no rule about when a fish decides to eat — a fish may eat several smaller fish one after another, or choose to eat none at all even when it could. When a fish eats a smaller one its own length does not change, and every gem in the smaller fish's stomach passes, undamaged, into the larger fish's stomach.

You are allowed to take a single fish out of the lake and keep all the gems currently in its stomach. Before setting out, you want to know how many different gem combinations you could obtain by catching one fish.

Write a program that, given the length of each fish and the kind of gem it originally swallowed, computes the number of different gem combinations that can end up in the stomach of some fish, modulo a given integer MM. A combination is determined solely by how many gems of each of the KK kinds it contains: gems have no order, and two gems of the same kind are indistinguishable.

Input

  • The first line contains the integer FF, the original number of fish in the lake (1≤F≤500,0001 \le F \le 500{,}000).
  • The second line contains the integer KK, the number of gem kinds. Kinds are numbered 11 through KK (1≤K≤F1 \le K \le F).
  • The third line contains the integer MM (2≤M≤30,0002 \le M \le 30{,}000).
  • Each of the next FF lines describes one fish with two space-separated integers: the fish's length followed by the kind of gem it originally swallowed (1≤LX≤1,000,000,0001 \le L_X \le 1{,}000{,}000{,}000).

It is guaranteed that at least one gem of each of the KK kinds is present.

Output

Print a single line containing one integer between 00 and M−1M-1 inclusive: the number of different possible gem combinations, taken modulo MM. The value of MM has no meaning beyond keeping the numbers small.

Examples3

  1. Example 1

    Input
    5
    3
    7
    2 2
    5 1
    8 3
    4 1
    2 3
    
    Expected output
    4
    
  2. Example 2

    Input
    1
    1
    2
    5 1
    
    Expected output
    1
    
  3. Example 3

    Input
    3
    1
    1000
    1 1
    2 1
    4 1
    
    Expected output
    3