This page is still under construction.

Parts of this page are still being built. What you see may change.

Reading

Time limit5sMemory limit128 MB

Summary
Count non-empty lowercase words whose total adjacent-letter difference is at most N, modulo 1e9+7.
Level

Medium7 of 10

Topics
Dynamic programming, Combinatorics
Solved
No attempts yet

Problem

A curious fact about the human brain is that, when we read, it mostly looks at the first and last letter of each word and fills in the rest — so a sentence whose inner letters are shuffled can still be read almost effortlessly.

Elly noticed that some shuffles read better than others: the letters l and i, or a and o, look much more alike than, say, x and m. She rates the difference between two letters on a scale from 11 to 55 — similar letters get a low value, very different letters a high one. Two equal letters always have difference 11.

The value of a word is the sum of the differences between every pair of adjacent letters. For example, if the difference between e and l is 33, between l and y is 22, and between i and l is 11, then the word elly has value 3+1+2=63 + 1 + 2 = 6 (recall that the equal pair l-l contributes 11). With those same differences the word lily has value 44, and a single letter such as i has value 00.

A longer word is not always worth more than a shorter one: lilii has value only 44, while elle has value 77. Still, every extra letter adds at least 11 to a word's value.

Elly wants to design a language that stays easy to read even with many misplaced letters, so she will include every non-empty word whose value is at most NN. Help her by counting how many such words there are.

Input

The first line contains two integers NN and MM — the largest allowed word value (1≤N≤1091 \le N \le 10^9) and the number of letter pairs for which a difference is defined.

Each of the next MM lines contains L1 L2 F, meaning the difference between the lowercase letters L1L1 and L2L2 is FF (1≤F≤51 \le F \le 5). The difference is symmetric, so it is the same from L1L1 to L2L2 and from L2L2 to L1L1. Every pair not listed has difference 11.

Output

Print a single integer — the number of non-empty words made of lowercase English letters whose value is at most NN. Because this count can be enormous, print it modulo 109+710^9 + 7.

Notes

As a bit of trivia, elleonora, entwine, and aaaaaaaaaaaaaaaaaaaaa are among the words that satisfy the condition.

Examples3

  1. Example 1

    Input
    20 10
    e l 3
    e o 1
    o n 2
    o r 4
    r a 4
    i n 5
    e n 2
    n t 3
    t w 3
    w i 5
    
    Expected output
    470059518
    
  2. Example 2

    Input
    1 0
    
    Expected output
    702
    
  3. Example 3

    Input
    2 0
    
    Expected output
    18278