What Does UNIST Stand For?

Time limit1sMemory limit512 MB

Summary
Count ways to pick a prefix of each of N words so the concatenation spells UNIST, modulo 1e9+7.
Level

Medium5 of 10

Topics
Dynamic programming, String, Hash map, Recursion
Solved
No attempts yet

Problem

UNIST stands for Ulsan National Institute of Science and Technology. One day, Woni wondered whether there are other words whose acronym is UNIST.

Let len⁡(a)\operatorname{len}(a) denote the length of a word aa. Given NN words W1,W2,…,WNW_1, W_2, \dots, W_N, for each word WiW_i (1≤i≤N)(1 \le i \le N), let PiP_i be a string obtained by taking between 0 and len⁡(Wi)\operatorname{len}(W_i) characters from the front of WiW_i. In other words, PiP_i is a prefix of WiW_i of length len⁡(Pi)\operatorname{len}(P_i).

Find the number of ways to choose PiP_i (1≤i≤N)(1 \le i \le N) such that P1+P2+⋯+PNP_1+P_2+\dots+P_N is UNIST. Here, the operation ++ is string concatenation.

Input

The first line gives the number of words NN.

The next NN lines give the NN words W1,W2,…,WNW_1, W_2, \dots, W_N, one per line.

Each WiW_i (1≤i≤N)(1 \le i \le N) is a string consisting of one or more uppercase English letters.

Output

Print the number of ways to choose P1,P2,…,PNP_1, P_2, \dots, P_N such that P1+P2+⋯+PNP_1+P_2+\dots+P_N is UNIST, modulo 1,000,000,007.

Constraints

  • 1≤N≤100,0001 \le N \le 100,000
  • len⁡(Wi)≤25\operatorname{len}(W_i) \le 25

Examples3

  1. Example 1

    Input
    7
    ULSAN
    NATIONAL
    INSTITUTE
    OF
    SCIENCE
    AND
    TECHNOLOGY
    
    Expected output
    1
    
  2. Example 2

    Input
    5
    UNICODE
    IS
    THE
    SPORTS
    TIME
    
    Expected output
    4
    
  3. Example 3

    Input
    2
    UNIS
    UT
    
    Expected output
    0