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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
#include <unordered_set>

const long long MOD = 1'000'000'000 + 7;

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

    int N, M;
    std::cin >> N >> M;

    std::vector<int> c(N);
    for(int i = 0; i < N; i++) {
        std::cin >> c[i];
    }

    std::vector<std::vector<int>> G(N);
    for(int i = 0; i < M; i++) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    long long result = 1;

    std::queue<int> Q;
    std::vector<bool> visited(N);
    for(int i = 0; i < N; i++) {
        if(visited[i]) {
            continue;
        }

        std::vector<int> comp;
        Q.push(i);
        visited[i] = true;
        while(!Q.empty()) {
            int v = Q.front();
            Q.pop();
            comp.push_back(v);
            for(int u : G[v]) {
                if(!visited[u]) {
                    visited[u] = true;
                    Q.push(u);
                }
            }
        }

        std::sort(comp.begin(), comp.end());
        auto id = [&](int v) {
            return std::lower_bound(comp.begin(), comp.end(), v) - comp.begin();
        };
        
        std::vector<std::pair<int, int>> switches;
        for(int v : comp) {
            for(int u : G[v]) {
                if(u < v) {
                    switches.push_back({id(v), id(u)});
                }
            }
        }

        int M = comp.size();
        std::unordered_set<int> used;

        int initial = 0;
        for(int v : comp) {
            if(c[v] == 1) {
                initial |= 1 << id(v);
            }
        }

        Q.push(initial);
        used.insert(initial);
        long long cnt = 0;
        while(!Q.empty()) {
            int mask = Q.front();
            Q.pop();
            cnt++;
            for(auto [a, b] : switches) {
                if(((mask >> a) & 1) != ((mask >> b) & 1)) {
                    continue;
                }
                int new_mask = mask ^ (1 << a) ^ (1 << b);
                if(used.count(new_mask) == 0) {
                    used.insert(new_mask);
                    Q.push(new_mask);
                }
            }
        }

        result = (result * cnt) % MOD;
    }

    std::cout << result << '\n';

    return 0;
}