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
#include <cstdio>
#include <cstdlib>
#include <cstdint>

#include <vector>

struct graph {
    struct edge {
        int vert;
        int next;

        edge(int vert, int next) : vert(vert), next(next) {}
    };

    std::vector<int> firsts;
    std::vector<edge> edges;

    graph(int count) {
        firsts.resize(count, -1);
    }
    graph(graph&&) = default;
    graph(const graph&) = delete;
    graph& operator=(graph&&) = default;
    graph& operator=(const graph&) = delete;

    void add_edge(int from, int to) {
        int idx = edges.size();
        edges.push_back(edge(to, firsts[from]));
        firsts[from] = idx;
    }

    template<typename F>
    void for_each_neighbor(int v, F f) const {
        for (int e = firsts[v]; e != -1; e = edges[e].next) {
            f(edges[e].vert);
        }
    }
};

static const uint64_t MODULUS = 1000 * 1000 * 1000 + 7;

enum class color : uint8_t {
    unknown = 0,
    red = 1,
    blue = 2,
};

std::vector<uint64_t> gen_factorials(uint64_t count) {
    std::vector<uint64_t> ret;
    ret.reserve(count);
    ret.push_back(1);
    for (uint64_t i = 1; i < count; i++) {
        ret.push_back((ret.back() * i) % MODULUS);
    }
    return ret;
}

constexpr uint64_t modular_inverse(const int64_t a) {
    int64_t oldr = a;
    int64_t r = MODULUS;
    int64_t olds = 1;
    int64_t s = 0;

    while (r != 0) {
        const int64_t q = oldr / r;
        const int64_t newr = oldr - q * r;
        const int64_t news = olds - q * s;
        oldr = r;
        olds = s;
        r = newr;
        s = news;
    }

    return (olds + MODULUS) % MODULUS;
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    std::vector<bool> lit_status;
    lit_status.reserve(n);

    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        lit_status.push_back(x != 0);
    }

    graph g(n);

    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        g.add_edge(a, b);
        g.add_edge(b, a);
    }

    uint64_t total = 1;

    auto factorials = gen_factorials(n + 1);

    auto binomial = [&factorials] (uint64_t n, uint64_t k) -> uint64_t {
        const uint64_t denominator = (factorials[k] * factorials[n - k]) % MODULUS;
        return (factorials[n] * modular_inverse(denominator)) % MODULUS;
    };

    std::vector<color> colors(n, color::unknown);
    std::vector<std::pair<int, color>> component_verts;
    for (int i = 0; i < n; i++) {
        if (colors[i] != color::unknown) {
            continue;
        }

        bool colorable = true;
        component_verts.push_back({i, color::red});
        colors[i] = color::red;

        int pos = 0;
        while (pos < component_verts.size()) {
            auto [v, col] = component_verts[pos++];
            color other_col = (col == color::red) ? color::blue : color::red;
            g.for_each_neighbor(v, [&] (int other) {
                if (colors[other] == color::unknown) {
                    colors[other] = other_col;
                    component_verts.push_back({other, other_col});
                } else if (colors[other] != other_col) {
                    colorable = false;
                }
            });
        }

        int reds_lit = 0;
        int reds_total = 0;
        int blues_lit = 0;
        int blues_total = 0;
        int parity = 0;
        for (auto [v, col] : component_verts) {
            if (col == color::red) {
                reds_total++;
                if (lit_status[v]) {
                    reds_lit++;
                }
            } else {
                blues_total++;
                if (lit_status[v]) {
                    blues_lit++;
                }
            }
            if (lit_status[v]) {
                parity = 1 - parity;
            }
        }

        // printf("Partition size: %llu, colorable: %s\n", (unsigned long long int)component_verts.size(), colorable ? "true" : "false");

        uint64_t contribution = 0;

        if (!colorable) {
            // printf(" parity: %d\n", parity);
            // All configurations with the right parity are good
            for (int p = parity; p <= component_verts.size(); p += 2) {
                // printf(" adding for p = %d: %llu\n", p, binomial(component_verts.size(), p));
                contribution = (contribution + binomial(component_verts.size(), p)) % MODULUS;
            }
        } else {
            // printf(" reds lit: %d/%d, blues lit: %d/%d\n", reds_lit, reds_total, blues_lit, blues_total);
            // The graph is bipartite. Every operation adds or removes a bulb in both parts.
            // We cannot go below 0 bulbs in either part or go above any part's capacity,
            // but otherwise configurations are unrestricted.
            const int begin = -std::min(reds_lit, blues_lit);
            const int end = std::min(reds_total - reds_lit, blues_total - blues_lit);
            for (int offp = begin; offp <= end; offp++) {
                const int r = reds_lit + offp;
                const int b = blues_lit + offp;
                // printf(" adding for r=%d/b=%d: %llu/%llu\n", r, b, binomial(reds_total, r), binomial(blues_total, b));
                const uint64_t count = (binomial(reds_total, r) * binomial(blues_total, b)) % MODULUS;
                contribution = (contribution + count) % MODULUS;
            }
        }

        component_verts.clear();

        total = (total * contribution) % MODULUS;
    }

    printf("%llu\n", total);

    return 0;
}