Magical Sort

시간 제한3초메모리 제한2048 MB

요약
n명의 순서가 모든 초기 배치와 길이에서 LSD 기수 정렬을 완성하게 하는 순서 개수를 선형형식과 초평면 구조로 세어 101287로 나눈 값을 출력합니다.
난이도

어려움10점 중 10점

유형
수학, 조합론, 정렬, 확률
정답자
아직 제출이 없습니다

문제

Once upon a time, there was a magician who had studied computer science for several years. One day, he was deeply impressed by the radix sort since it felt like real magic to him. He decided to use a similar mechanism to invent a new card shuffling magic trick. However, he had to make it more sophisticated so that no one would notice anything until the very end of the performance. Here’s the trick he wrote down in his notebook:

  1. There are a pile of cards and nn assistants.

  2. Let someone from the audience do the following:

    1. Choose any integer K≥1K ≥ 1.
    2. Take any number of cards from the pile and write down one KK-digit binary number on the front side of each of the cards. The same binary number may be written on two or more cards.
    3. Distribute the cards to the nn assistants in any arbitrary way. Note that the assistants might receive a different number of cards, possibly even zero.
  3. For k=1,…,Kk = 1, \dots ,K, repeat the following:

    1. The assistants pick up the pile of cards in front of themselves.

    2. I ask each assistant in the pre-specified “work order” to do the following:

      1. Divide the cards in his hand into two groups according to the kk-th least significant bit, i.e., Group-00 for the cards with 00-bit and Group-11 for those with 11-bit, while preserving their original order.

      2. Stack the cards in front of other assistants (or possibly themselves) according to the “distribution table” which describes who gives which group of cards to whom (see, e.g., Table 1). The cards should be faced down so that the cards in front of each assistant can be collected in the work order of the assistants who gave the cards to them.

        AssistantADABOBJOHNMAXZOE
        Work order5522331144
        Receiver of Group-00 cardsJOHNMAXJOHNBOBJOHN
        Receiver of Group-11 cardsADAZOEZOEZOEZOE
  4. After KK rounds, I collect the cards in the work order of assistants.

Since the audience may choose any KK and write any length-KK binary numbers without control, the magician carefully designed the distribution table and the work order so that the binary numbers can always be sorted in non-decreasing lexicographical order at the end of the above procedure regardless of the initial configuration.

The magician had a rehearsal with the distribution table in Table 1. At the beginning K=3K = 3 was chosen each assistant received the cards as described in the first two rows in Table 2; note that the assistants are written in their work order, in which they perform Step-3b described above. The third and fourth rows of Table 2 show how the cards were divided at the first round. For example, MAX divided the cards into Group-00 (010,100)(010, 100) and Group-11 (111,011)(111, 011), after which he put Group-00 cards in front of BOB, and Group-11 cards in front of ZOE.

AssistantMAXBOBJOHNZOEADA
Cards (initial)010,111,011,100010, 111, 011, 100001,000001, 000-000000110,111110, 111
Group-00 cards010,100010, 100000000-000000110110
Group-11 cards111,011111, 011001001--111111
Cards after Round 11000000010,100010, 100000,110000, 110111,011,001111, 011, 001111111

The last row of Table 2 shows the stacked cards in front of each assistant at the end of the first round. For example, ZOE received three cards (111,011,001)(111, 011, 001): two cards (111,011)(111, 011) from MAX, and the other card (001)(001) from BOB. And they were collected in the work order of the assistants who gave the cards.

The other two rounds were performed similarly, and the following table shows the cards stacked in front of each assistant at the end of the two rounds. Notice that after Round 33, one can obtain the binary numbers in non-decreasing lexicographical order by collecting the cards in work order of the assistants.

AssistantMAXBOBJOHNZOEADA
Cards after Round 22100100000000000,001000, 001010,110,111,011010, 110, 111, 011111111
Cards after Round 33000000-000,001,010,011000, 001, 010, 011100,110,111100, 110, 111111111

Once day, the magician noticed that there might exist more than one work orders for the same distribution table with which the above procedure always ends up with the binary numbers sorted correctly. For example, the procedure still works well even if BOB and MAX are swapped in the work order. The magician wants to know how many such work orders exist for his distribution table.

Given a distribution table, write a program to find the number of work orders with which the procedure works correctly in every case, regardless of the value of KK and the binary numbers written on the cards. It is guaranteed that at least one such work order exists for the distribution table.

입력

Your program is to read from standard input. The input starts with a line containing an integer nn (2≤n≤1052 ≤ n ≤10^5), where nn is the number of assistants. Then the distribution table is given in the following nn lines. Each line contains the names of three assistants XX, YY, ZZ, meaning that at each round kk, XX gives the cards with 00-bit at the kk-th least significant bit to YY, and those with 11-bit to ZZ. Each name is a non-empty string consisting of up to four English upper letters. Every assistant appears as a receiver at least once in the table.

출력

Your program is to write to standard output. Print exactly one number WW modulo 101,287101\\,287 where W≥1W ≥ 1 is the number of work orders with which it is always possible to sort equal-length binary numbers in non- decreasing lexicographical order using the described procedure with the given distribution table.

예제3

  1. 예제 1

    입력
    5
    ADA JOHN ADA
    BOB MAX ZOE
    JOHN JOHN ZOE
    MAX BOB ZOE
    ZOE JOHN ZOE
    
    예상 출력
    2
    
  2. 예제 2

    입력
    2
    ADA BOB ADA
    BOB BOB ADA
    
    예상 출력
    1
    
  3. 예제 3

    입력
    8
    A A E
    B A G
    C A F
    D A H
    E A H
    F C H
    G B H
    H D H
    
    예상 출력
    4