Under construction

Still being built. Things may change

Rock Paper Scissors Strategy

Time limit1sMemory limit1024 MB

Summary
Given N players and M games with participant lists and winners, count the strategy assignments (fixed hand, or cyclic RPS pattern) consistent with all results, modulo 1e9+7.
Level

Hard8 of 10

Topics
Implementation, Brute force, Math
Solved
No attempts yet

Problem

Yunee, the president of the UNIST Rock-Paper-Scissors Association, held a rock-paper-scissors contest for UNIST students and gathered NN participants. The contest consists of MM games in total. In each game, two or three participants make one of the three hand gestures, rock, paper, or scissors, all at once. Then the winner of each game is determined according to the following rules.

  • Rock wins scissors, scissors win paper, and paper wins rock.
  • If everyone makes the same hand gesture, the game draws.
  • If all three types of hand gestures appear, the game draws.
  • If the game doesn't draw, everyone who makes a winning hand gesture wins.

Each UNIST student had prepared one of the following strategies for this contest.

  • Always play the same hand gesture.
  • Play the three hand gestures once each, in any order. Repeat the same pattern for every three games this player participates in.

Unfortunately, Yunee couldn't watch the contest because of a lot of paperwork. But Yunee has a document that lists the participants and the winners of each game. Yunee wondered what strategies the students might have used. Find the number of possible strategy combinations of all students, modulo 109+710^9+7.

Input

The first line contains two integers NN and MM (3≤N≤300,1≤M≤300)(3 \leq N \leq 300, 1 \leq M \leq 300). NN represents the number of participants of the contest. MM represents the number of games.

The next MM lines contain the descriptions for the MM games in chronological order. Each game is described as follows.

  • The number of participants aa is given. (a=2 or a=3)(a=2\text{ or }a=3)
  • The participants' numbers p_1,⋯ ,p_ap\_1, \cdots, p\_a are given. They are distinct integers ranging from 11 to NN.
  • A delimiter / is given.
  • The number of winners bb is given. If the game draws, b=0b = 0. (0≤b<a)(0\leq b < a)
  • The winners' numbers q_1,⋯ ,q_bq\_1, \cdots, q\_b are given. They are distinct integers among p_1,⋯ ,p_ap\_1, \cdots, p\_a.

Output

Output the number of possible strategy combinations of all students, modulo 109+710^9+7.

Examples3

  1. Example 1

    Input
    3 3
    2 1 2 / 1 1
    2 2 3 / 1 2
    3 1 2 3 / 0
    
    Expected output
    27
    
  2. Example 2

    Input
    3 1
    2 1 2 / 0
    
    Expected output
    243
    
  3. Example 3

    Input
    3 3
    2 1 2 / 1 1
    2 2 1 / 1 1
    2 1 2 / 0
    
    Expected output
    0