1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <bits/stdc++.h>

using namespace std;


long long BIG_PRIME = 1e9+7;


struct Node {
    int id;
    vector<int> neighbours;
};


map<pair<int, int>, long long> binomial_cache;

map<int, long long> power_cache;

long long my_power(int exponent) {
    if (exponent == 0) {
        return 1;
    }
    if (power_cache.find(exponent) != power_cache.end()) {
        return power_cache[exponent];
    }
    long long result;
    if (exponent % 2 == 0) {
        result = my_power(exponent / 2) * my_power(exponent / 2) % BIG_PRIME;
    } else {
        result = my_power((exponent - 1) / 2) * my_power((exponent - 1)/2) * 2 % BIG_PRIME;
    }

    power_cache[exponent] = result;
    return result;
}

long long BinomialCoefficient(int n, int k) {
    if (k == 0 or k == n) {
        return 1;
    }
    if (binomial_cache.find({n, k}) != binomial_cache.end()) {
        return binomial_cache[{n, k}];
    }
    long long result = (BinomialCoefficient(n - 1, k - 1) + BinomialCoefficient(n - 1, k)) % BIG_PRIME;
    binomial_cache[{n, k}] = result;
    return result;
}

vector<vector<int>> value_by_parity(vector<Node> &lights, int start, string position) {
    queue<pair<int, int>> q;
    vector<vector<int>> result(2, vector<int>(2, 0));

    q.push({start, 0});
    vector<bool> visited(lights.size(), false);
    visited[start] = true;
    while (!q.empty()) {
        auto [current, parity] = q.front();
        q.pop();
        result[parity][position[current] == '0' ? 0 : 1]++;
        for (int neighbour : lights[current].neighbours) {
            if (!visited[neighbour]) {
                q.push({neighbour, parity ^ 1});
                visited[neighbour] = true;
            }
        }
    }
    return result;
}


vector<pair<Node, int>> split_graph(vector<Node> &lights) {
    vector<pair<Node, int>> result;
    vector<int> groups(lights.size(), -1);

    for (Node node : lights) {
        if (groups[node.id] == -1) {
            queue<int> q;
            q.push(node.id);
            groups[node.id] = node.id;
            int size = 1;
            while (!q.empty()) {
                Node current = lights[q.front()];
                q.pop();
                for (int neighbour_id : current.neighbours) {
                    if (groups[neighbour_id] == -1) {
                        groups[neighbour_id] = node.id;
                        size++;
                        q.push(neighbour_id);
                    }
                }
            }
            result.push_back({node, size});
        }
    }

    return result;
}


bool find_odd_cycle(Node node, vector<Node> &lights) {
    queue<int> q;
    vector<int> groups(lights.size(), -1);
    q.push(node.id);
    groups[node.id] = 0;
    while (!q.empty()) {
        Node current = lights[q.front()];
        q.pop();
        for (int neighbour_id : current.neighbours) {
            if (groups[neighbour_id] == -1) {
                groups[neighbour_id] = groups[current.id] == 0 ? 1 : 0;
                q.push(neighbour_id);
            } else if (groups[neighbour_id] == groups[current.id]) {
                return true;
            }
        }
    }
    return false;
}


long long solve_compact(Node node, int size, string current, vector<Node> &lights) {
    // cout << "Solving " << node.id << " " << size << endl;
    if (size == 1) {
        return 1;
    }

    // cout << "Solving " << node.id << " " << size << endl;
    if (size > 2 && find_odd_cycle(node, lights)) {
        return my_power(size - 1);
    }
    vector<vector<int>> value = value_by_parity(lights, node.id, current);
    // cout << "Parity" << endl;
    // cout << value[0][0] << " " << value[0][1] << endl;
    // cout << value[1][0] << " " << value[1][1] << endl;

    if (value[0][0] + value[1][1] > value[0][1] + value[1][0]) {
        swap(value[0][0], value[0][1]);
        swap(value[1][0], value[1][1]);
    }

    int big_start = value[0][0] + value[1][1];
    int bigger = max(value[0][0] + value[0][1], value[1][0] + value[1][1]);
    int smaller = min(value[0][0] + value[0][1], value[1][0] + value[1][1]);

    long long result = 0;
    int i = 0;
    while(true) {
        // cout << "Binomial " << bigger << " " << big_start - i << endl;
        // cout << "Binomial " << smaller << " " << i << endl;
        result += BinomialCoefficient(bigger, big_start - i)
                * BinomialCoefficient(smaller, i);
        result %= BIG_PRIME;
        if (i == smaller or big_start - i == 0) {
            break;
        }
        i++;
    }

    return result;
    // return brute(current, node, lights);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);

    int n, m;

    cin >> n >> m;

    vector<Node> lights;
    for (int i = 0; i < n; i++) {
        Node node = Node();
        node.id = i;
        lights.push_back(node);
    }

    string current = "";
    for (int i = 0; i < n; i++) {
        string c;
        cin >> c;
        current += c;
    }

    // cout << current << endl;
    // cout << "blocked: " << compress(current).size() << " - " << compress(current) << endl;

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        // lights[a - 1].neighbours.push_back(lights[b - 1]);
        // lights[b - 1].neighbours.push_back(lights[a - 1]);
        lights[a - 1].neighbours.push_back(b - 1);
        lights[b - 1].neighbours.push_back(a - 1);

        // cout << "neighbours " << a << " " << b << endl;
    }

    vector<pair<Node, int>> splited = split_graph(lights);
    // cout << "OK" << endl;
    long long result = 1;
    for (auto [node, size] : splited) {
        // cout << "Splited " << node.id << " " << size << endl;
        long long tmp = solve_compact(node, size, current, lights);
        // cout << tmp << endl;
        result *= tmp;
        result %= BIG_PRIME;
    }


    cout << result << endl;

    return 0;
}