Black Widow
Contest: Codeforces Round 366 (Div. 1)
Difficulty rating: 2900
Tags: dp, graphs, implementation, math

Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form:

$$( v _ { 1, 1 } \vee \ldots \vee v _ { 1, k _ { 1 } } ) \oplus ( v _ { 2, 1 } \vee \ldots \vee v _ { 2, k _ { 2 } } ) \oplus \ldots \oplus ( v _ { n, 1 } \vee \ldots \vee v _ { n, k _ { n } } ) = 1$$

Where $$\mathrm{~V~}$$ represents logical OR and $$\bigcirc$$ represents logical exclusive OR (XOR), and vi, j are some boolean variables or their negations. Natalia calls the left side of the equation a XNF formula. Each statement in brackets is called a clause, and vi, j are called literals.

In the equation Natalia has, the left side is actually a 2-XNF-2 containing variables x1, x2, ..., xm and their negations. An XNF formula is 2-XNF-2 if:

1. For each 1 ≤ i ≤ n, ki ≤ 2, i.e. the size of each clause doesn't exceed two.
2. Each variable occurs in the formula at most two times (with negation and without negation in total). Please note that it's possible that a variable occurs twice but its negation doesn't occur in any clause (or vice versa).

Natalia is given a formula of m variables, consisting of n clauses. Please, make sure to check the samples in order to properly understand how the formula looks like.

Natalia is more into fight than theory, so she asked you to tell her the number of answers to this equation. More precisely, you need to find the number of ways to set x1, ..., xm with true and false (out of total of 2m ways) so that the equation is satisfied. Since this number can be extremely large, you need to print the answer modulo 109 + 7.

Please, note that some variable may appear twice in one clause, or not appear in the equation at all (but still, setting it to false or true gives different ways to set variables).

Input format:
The first line of input contains two integers n and m (1 ≤ n, m ≤ 100 000) — the number of clauses and the number of variables respectively.

The next n lines contain the formula. The i-th of them starts with an integer ki — the number of literals in the i-th clause. It is followed by ki non-zero integers ai, 1, ..., ai, ki. If ai, j > 0 then vi, j is xai, j otherwise it's negation of x - ai, j (1 ≤ ki ≤ 2,  - m ≤ ai, j ≤ m, ai, j ≠ 0).

Output format:
Print the answer modulo 1 000 000 007 (109 + 7) in one line.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The equation in the first sample is:

$$( x _ { 4 } \vee \overline { { x _ { 2 } } } ) \oplus ( x _ { 6 } \vee x _ { 3 } ) \oplus ( \overline { { x _ { 7 } } } \vee x _ { 1 } ) \oplus ( \overline { { x _ { 5 } } } \vee x _ { 6 } ) \oplus ( \overline { { x _ { 2 } } } \vee \overline { { x _ { 5 } } } ) = 1$$

The equation in the second sample is:

$$( \overline { x _ { 5 } } ) \oplus ( x _ { 4 } \lor \overline { x _ { 6 } } ) \oplus ( \overline { x _ { 2 } } \lor \overline { x _ { 6 } } ) \oplus ( \overline { x _ { 7 } } \lor x _ { 9 } ) \oplus ( x _ { 1 0 } \lor \overline { x _ { 1 } } ) \oplus ( x _ { 3 } \lor \overline { x _ { 1 } } ) \oplus ( \overline { x _ { 8 } } \lor x _ { 9 } ) \oplus ( x _ { 5 } \lor x _ { 8 } ) = 1$$

The equation in the third sample is:

$$( x _ { 1 } \vee x _ { 1 } ) \oplus ( \overline { x _ { 3 } } \vee x _ { 3 } ) = 1$$

Example input:
6 7
2 4 -2
2 6 3
2 -7 1
2 -5 1
2 3 6
2 -2 -5

Example output:
48

Example input:
8 10
1 -5
2 4 -6
2 -2 -6
2 -7 9
2 10 -1
2 3 -1
2 -8 9
2 5 8

Example output:
544

Editorial: