Under construction

Still being built. Things may change

Word Chain

Time limit1sMemory limit1024 MB

Summary
Given a dictionary, for each starting word find the win probabilities for each bear, the chance Tokki wins by an endless game, and the expected word count, all mod 998244353.
Level

Medium7 of 10

Topics
Graph, Probability, Matrix, Math
Solved
No attempts yet

Problem

The White Bear and the Black Bear received a dictionary with NN words from Tokki. The two bears like to play word chain. In word chain, the two bears take turns saying words from the dictionary. A bear may only say a word that starts with the last letter of the word the other bear said just before. If at least one valid word exists, the bear picks one of them at random, and every valid word has the same probability of being picked. If no valid word exists, the game ends. The bear who said the last word wins. A word in the dictionary can be said many times, so the game may never end. Each time a bear says a word, Tokki decides whether the game can end. If the game cannot end no matter which words the two bears say afterward, Tokki ends the game and wins.

For each integer ii from 11 to NN, let αi\alpha_i be the probability that the White Bear wins when the game starts with the White Bear saying the ii-th word of the dictionary, βi\beta_i the probability that the Black Bear wins, γi\gamma_i the probability that Tokki wins, and δi\delta_i the expected number of words said by the two bears until the game ends. The White Bear, the Black Bear, and Tokki compare the values computed mathematically with the values α^,β^,γ^,δ^\hat\alpha, \hat\beta, \hat\gamma, \hat\delta obtained by actually playing the game. Find αi,βi,γi,δi\alpha_i,\beta_i,\gamma_i,\delta_i for every integer ii from 11 to NN.

Input

The first line contains the number of words NN in the dictionary. (1≤N≤2×105)(1\le N\le 2\times 10^5) From the second line onward, the ii-th of the next NN lines contains the ii-th word wiw_i of the dictionary. (1≤∣wi∣≤10;(1\le\lvert w_i\rvert\le 10; i≠j  ⟹  wi≠wj)i\neq j\implies w_i\neq w_j) Every word consists of uppercase Roman letters.

Output

Print NN lines. On line ii, print αi,βi,γi,δi\alpha_i,\beta_i,\gamma_i,\delta_i, each taken modulo 998 244 353998\,244\,353 and separated by spaces. Strictly speaking, write each rational number as PQ\frac{P}{Q}, where P≥0P\ge 0, Q>0Q>0, and gcd⁡(P,Q)=1\gcd(P,Q)=1 are integers. Print the unique integer RR with 0≤R<998 244 3530\le R<998\,244\,353 that satisfies R×Q≡P(mod998 244 353)R\times Q\equiv P\pmod{998\,244\,353}.

Examples3

  1. Example 1

    Input
    6
    BIRD
    DOLPHIN
    DUCK
    KIWI
    DOG
    N
    
    Expected output
    332748118 332748118 332748118 332748120
    0 0 1 1
    0 1 0 2
    1 0 0 1
    1 0 0 1
    0 0 1 1
    
  2. Example 2

    Input
    5
    MELON
    NEW
    WHALE
    RACCOON
    WATER
    
    Expected output
    665496236 332748118 0 6
    332748118 665496236 0 5
    1 0 0 1
    665496236 332748118 0 6
    332748118 665496236 0 7
    
  3. Example 3

    Input
    10
    BEAR
    BELUGA
    BETA
    TUNA
    TAB
    TIGER
    RETRIEVER
    RABBIT
    PENGUIN
    NOTATION
    
    Expected output
    159719097 838525257 0 7
    1 0 0 1
    1 0 0 1
    1 0 0 1
    279508419 718735935 0 4
    159719097 838525257 0 7
    159719097 838525257 0 7
    519087064 479157290 0 5
    0 0 1 1
    0 0 1 1