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
// PA2024 runda 5C -  https://sio2.mimuw.edu.pl/c/pa-2024-1/p/zar/
//-std=c++20
#include<iostream>
#include <cstddef>
#include <algorithm>
#include<vector>
#include <random> // Added for random number generation
#include <list>
#include<map>

using I = int64_t;
using namespace std;

struct Vertex {
    int8_t state;
    list<Vertex *> edges;
    I sub_graph_no = 0;
    bool xor_visited = false;

    bool blocked() {
        bool blocked = true;
        for (const auto &v: edges) {
            blocked = blocked && v->state != state;
        }
        return blocked;
    }
};

struct Task {
    I v, e;
    vector<Vertex> vertexes;
    vector<Vertex *> subgraphs;
    vector<I> subgraph_sizes;
    vector<bool> subgraph_valid;

    void run() {
        input_data();
        calculate_subgraphs();
        calculate_validity();
        I result = 1;
        for (I i = 0; i < subgraphs.size(); ++i) {
            if (subgraph_valid[i]) {
                for (I j = 1; j <= subgraph_sizes[i] - 1; j++) {
                    result = (result * 2) % 1'000'000'007;
                }
            }
        }
        cout << result<<'\n';
    }

    void calculate_validity() {
        subgraph_valid.resize(subgraphs.size());
        for (I i = 0; i < subgraphs.size(); i++) {
            subgraph_valid[i] = !traverse_blocked(subgraphs[i]);
        }
    }

    bool traverse_blocked(Vertex *vertex) {
        bool blocked = vertex->blocked();
        vertex->xor_visited = true;
        for (const auto &item: vertex->edges) {
            if (!item->xor_visited) {
                blocked &= traverse_blocked(item);
            }
        }
        return blocked;
    }

    void calculate_subgraphs() {
        I sub_graph_counter = 0;
        auto it = vertexes.begin();
        while (it != vertexes.end()) {
            if (it->sub_graph_no == 0) {
                Vertex *vertex = &(*it);
                sub_graph_counter++;
                I size = traverse_subgraph(vertex, sub_graph_counter);
                subgraphs.push_back(vertex);
                subgraph_sizes.push_back(size);
            }
            it++;
        }
    }

    I traverse_subgraph(Vertex *vertex, I s) {
        vertex->sub_graph_no = s;
        I size = 1;
        for (const auto &item: vertex->edges) {
            if (item->sub_graph_no == 0) {
                size += traverse_subgraph(item, s);
            }
        }
        return size;
    }

    void input_data() {
        cin >> v >> e;
        vertexes.resize(v);
        for (int i = 0; i < v; i++) {
            int x;
            cin >> x;
            if (x) {
                vertexes[i].state = 1;
            } else {
                vertexes[i].state = -1;
            }
        }
        for (int i = 0; i < e; ++i) {
            I x, y;
            cin >> x >> y;
            x--;
            y--;
            vertexes[x].edges.push_back(&vertexes[y]);
            vertexes[y].edges.push_back(&vertexes[x]);
        }
    }
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Task x;
    x.run();
}