Under construction

Still being built. Things may change

True/False Worksheet

Time limit2sMemory limit512 MB

Summary
Count binary strings of length n that satisfy range hints, where each hint says a range is all equal or not all equal, modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Intervals, Implementation
Solved
No attempts yet

Problem

Bob is completing a true/false worksheet, consisting of a list of n problems where each answer is either “true” or “false”. The problems are numbered from 1 to n. They are too hard for Bob, so the TA, Alice, has given Bob m hints. For each hint i, Alice gives Bob an (inclusive) range of questions [li, ri], and tells him either “all answers in the range are the same” (in other words, either all are “true”, or all are “false”); or “not all of the answers in the range are the same.” Help Bob count how many different answer sequences satisfy the given hints. Since this number may be huge, print the answer modulo 109 + 7.

Input

The first line of the input contains two space-separated integers n and m (1 ≤ n ≤ 5 000, 1 ≤ m ≤ 1 000 000), the number of problems and number of hints, respectively. The next m lines each encode a hint, and contain two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) followed by either the word same, if all answers in the range are the same, or different, if all answers in the range are not the same (i.e., at least one answer is “true” and at least one other answer is “false”).

Output

Print the number of different answer sequences satisfying all the hints, modulo 109 + 7.

Notes

In the first sample, the four possible sequences consistent with the hints are 00000, 10000, 01111, and 11111 where 0 stands for a “false” answer and 1 stands for a “true” answer. In the second sample, the third hint conflicts with the first two hints, so no answer sequence exists consistent with all hints.

Examples2

  1. Example 1

    Input
    5 2
    2 4 same
    3 5 same
    
    Expected output
    4
    
  2. Example 2

    Input
    5 3
    1 3 same
    2 5 same
    1 5 different
    
    Expected output
    0