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
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

#define pb push_back

typedef long long ll;
typedef long double ld;
int n, m;
const int maxN = 200'005;
const int maxM = 400'005;
int c[maxN];
vector<int> g[maxN];
int clr[maxN];
int C[2] = {0, 0};
const int mod = (int)1e9 + 7;
int mult(int a, int b) {
    return (1LL * a * b) % mod;
}
int sub(int a, int b) {
    int s = a - b;
    if (s < 0) {
        s += mod;
    }
    return s;
}
int fact[maxN], invfact[maxN], inv[maxN];
bool HAS_CYC = false;
int pw2[maxN];
bool used[maxN];
void dfs(int v) {
    C[clr[v] ^ c[v]]++;
    used[v] = true;
    for (int to : g[v]) {
        if (!used[to]) {
            clr[to] = clr[v] ^ 1;
            dfs(to);
        }
        else if (clr[to] != (clr[v] ^ 1)) {
            HAS_CYC = true;
        }
    }
}
int cnk(int n, int k) {
    if (n < k || k < 0) return 0;
    return mult(fact[n], mult(invfact[k], invfact[n - k]));
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    cin >> n >> m;
    fact[1] = invfact[1] = inv[1] = fact[0] = invfact[0] = 1;
    pw2[0] = 1;
    pw2[1] = 2;
    for (int i = 2; i < maxN; i++) {
        pw2[i] = mult(2, pw2[i - 1]);
        fact[i] = mult(i, fact[i - 1]);
        inv[i] = mult(inv[mod % i], sub(0, mod / i));
        invfact[i] = mult(invfact[i - 1], inv[i]);
    }
    for (int i = 1; i <= n; i++) {
        clr[i] = -1;
        cin >> c[i];
    }
    for (int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            C[0] = C[1] = 0;
            HAS_CYC = false;
            clr[i] = 0;
            dfs(i);
            if (HAS_CYC) {
                ans = mult(ans, pw2[C[0] + C[1] - 1]);
            }
            else {
                ans = mult(ans, cnk(C[0] + C[1], C[0]));
            }
        }
    }
    cout << ans;
    return 0;
}