Problem:
Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    class Program
    {
        static void Main(string[] args)
        {
            ABC128.C();
        }



        class ABC128
        {
            public static void A()
            {
                var A = Read.Int();
                var P = Read.Int();
                Console.WriteLine((A * 3 + P) / 2);
            }
            public static void B()
            {
                var N = Read.Int();
                var dict = new Dictionary<string, List<Tuple<int, int>>>();
                for (int n = 1; n <= N; ++n)
                {
                    var s = Read.Str();
                    var p = Read.Int();
                    if (!dict.ContainsKey(s)) { dict.Add(s, new List<Tuple<int, int>>()); }
                    dict[s].Add(new Tuple<int, int>(p, n));
                }
                foreach (var kvp in dict.OrderBy(x => x.Key))
                {
                    var ps = kvp.Value.OrderBy(x => -x.Item1).ToArray();
                    foreach (var p in ps)
                    {
                        Console.WriteLine(p.Item2);
                    }
                }
            }
            public static void C()
            {
                var N = Read.Int();
                var M = Read.Int();
                var S = new HashSet<int>[M];
                for (int i = 0; i < M; ++i)
                {
                    S[i] = new HashSet<int>();
                    var s = Read.IntN();
                    foreach (var ss in s.Skip(1))
                    {
                        S[i].Add(ss - 1);
                    }
                }
                var P = Read.IntN();
                int count = 0;
                for (int i = 0; i < (1 << N); ++i)
                {
                    bool ok = true;
                    for (int m = 0; m < M; ++m)
                    {
                        var on = 0;
                        foreach (var n in S[m])
                        {
                            if (((1 << n) & i) > 0) { on++; }
                        }
                        if (on % 2 != P[m]) { ok = false; break; }
                    }
                    if (ok) { count++; }
                }
                Console.WriteLine(count);
            }
            public static void D()
            {
                var N = Read.Int();
                var K = Read.Int();
                var V = Read.LongN();
                long max = 0;
                for (int left = 0; left <= N; ++left)
                {
                    for (int right = 0; right <= N - left; ++right)
                    {
                        if (left + right > K) { break; }
                        List<long> list = new List<long>();
                        for (int i = 0; i < left; ++i) { list.Add(V[i]); }
                        for (int i = 0; i < right; ++i) { list.Add(V[N - 1 - i]); }
                        list = list.OrderBy(x => x).ToList();
                        long sum = 0;
                        int drop = K - left - right;
                        for (int i = 0; i < list.Count; ++i)
                        {
                            sum += i < drop ? Math.Max(0, list[i]) : list[i];
                        }
                        max = Math.Max(max, sum);
                    }
                }
                Console.WriteLine(max);
            }
            public static void E()
            {
                var N = Read.Int();
                var Q = Read.Int();
                var STX = new Tuple<long, long, long>[N];
                for (int i = 0; i < N; ++i)
                {
                    var v = Read.LongN();
                    STX[i] = new Tuple<long, long, long>(v[0], v[1], v[2]);
                }
                var D = Enumerable.Range(0, Q).Select(i => Read.Long()).ToArray();

            }
            public static void F()
            {

            }
        }




        class Util
        {
            public static long[] ASum(long[] array)
            {
                var res = new long[array.Length + 1];
                for (int i = 0; i < array.Length; ++i)
                {
                    res[i + 1] = res[i] + array[i];
                }
                return res;
            }
        }

        static class Read
        {
            static Queue<string> queue = new Queue<string>();
            static void Enqueue() { var ss = Console.ReadLine().Split(' '); foreach (var s in ss) { queue.Enqueue(s); } }
            public static int Int() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt32(queue.Dequeue()); }
            public static long Long() { if (queue.Count == 0) { Enqueue(); } return Convert.ToInt64(queue.Dequeue()); }
            public static string Str() { if (queue.Count == 0) { Enqueue(); } return queue.Dequeue(); }
            public static int[] IntN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray(); }
            public static long[] LongN() { return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray(); }
            public static string[] StrN() { return Console.ReadLine().Split(' '); }
            public static Tuple<int, int>[] TupleInt(int n) { List<Tuple<int, int>> list = new List<Tuple<int, int>>(); for (int i = 0; i < n; ++i) { var ab = Read.IntN(); list.Add(new Tuple<int, int>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<long, long>[] TupleLong(int n) { List<Tuple<long, long>> list = new List<Tuple<long, long>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<long, long>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<string, string>[] TupleStr(int n) { List<Tuple<string, string>> list = new List<Tuple<string, string>>(); for (int i = 0; i < n; ++i) { var ab = Read.StrN(); list.Add(new Tuple<string, string>(ab[0], ab[1])); } return list.ToArray(); }
            public static Tuple<double, double>[] TupleDouble(int n) { List<Tuple<double, double>> list = new List<Tuple<double, double>>(); for (int i = 0; i < n; ++i) { var ab = Read.LongN(); list.Add(new Tuple<double, double>(ab[0], ab[1])); } return list.ToArray(); }
            public static long[,] LongMatrix(int r, int c)
            {
                var mat = new long[r, c];
                for (int i = 0; i < r; ++i)
                {
                    var x = Read.LongN();
                    for (int j = 0; j < c; ++j)
                    {
                        mat[i, j] = x[j];
                    }
                }
                return mat;
            }
            public static Dictionary<int, HashSet<int>> AdjacencyList(int N, int M)
            {
                Dictionary<int, HashSet<int>> dict = new Dictionary<int, HashSet<int>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new HashSet<int>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1]);
                    dict[ab[1]].Add(ab[0]);
                }
                return dict;
            }
            public static Dictionary<int, Dictionary<int, long>> AdjacencyListWithWeight(int N, int M)
            {
                Dictionary<int, Dictionary<int, long>> dict = new Dictionary<int, Dictionary<int, long>>();
                for (int i = 0; i <= N; ++i)
                {
                    dict.Add(i, new Dictionary<int, long>());
                }
                for (int i = 0; i < M; ++i)
                {
                    var ab = Read.IntN();
                    dict[ab[0]].Add(ab[1], ab[2]);
                    dict[ab[1]].Add(ab[0], ab[2]);
                }
                return dict;
            }
        }
    }
}