Under construction

Still being built. Things may change

Metal Is Life

Time limit2sMemory limit512 MB

Summary
Count permutations of N distinct strings that satisfy up to 8 prefix constraints between fixed positions, modulo 1e9+7.
Level

Medium7 of 10

Topics
Combinatorics, Bit manipulation, Dynamic programming, Sorting
Solved
No attempts yet

Problem

Hello everyone
This is Kangto Lee of Watch Out Records
Metal is life
Do not even think about listening to anything else??

Kangto Lee (Watch Out Records)

Kangto goes around shouting that metal is life. One day he got very bored, so he wrote the names of his N favorite bands on cards, one name per card. He stacked the cards in the order he likes the bands and was handing the stack to Nana when he coughed, and the order got completely scrambled.

Nana wants to sort the cards back into Kangto's order of preference. Kangto told her only M facts before running off, so those M facts are the only clues she has.

Call the stack sorted in Kangto's order of preference A. The first card is A0A_0, the second card is A1A_1, and the last card is AN−1A_{N-1}. A uses each of the N band names exactly once. Each fact consists of two integers u and v, and it means that AuA_u was a prefix of AvA_v. A string x is a prefix of a string y when y starts with x.

Write a program that counts the possible sequences A. Some of the facts Kangto gave may be false, so there may be no possible A at all.

Input

The first line contains N and M. (2≤N≤502 \le N \le 50, 0≤M≤80 \le M \le 8)

The second line contains the N band names Kangto likes, separated by spaces. Each name consists of lowercase letters only and its length is at most 50. The names are distinct. The order in which they are given has nothing to do with Kangto's order of preference.

Each of the next M lines contains one fact Kangto gave, u and v. (0≤u,v≤N−10 \le u, v \le N-1, u≠vu \ne v) The same fact is never given more than once.

Output

Print the number of possible sequences A modulo 1,000,000,007.

Examples5

  1. Example 1

    Input
    3 1
    kangto kangtoro kang
    0 1
    
    Expected output
    3
    
  2. Example 2

    Input
    4 2
    kangto kangho kangdo kang
    0 1
    0 2
    
    Expected output
    6
    
  3. Example 3

    Input
    3 2
    kangto nana watchoutrecords
    0 1
    1 0
    
    Expected output
    0
    
  4. Example 4

    Input
    7 4
    kang watch na kangto watchout nana watchoutrecords
    0 1
    1 2
    3 4
    5 6
    
    Expected output
    2
    
  5. Example 5

    Input
    3 0
    remnantsofthefallen eighteenapril satellights
    
    Expected output
    6