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
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
// clang-format on

// clang-format off
const int MOD = 1000000007;
int add(const int& a, const int& b) { return (a + b >= MOD ? a + b - MOD : a + b); }
int mul(const int& a, const int& b) { return 1LL * a * b % MOD; }
int fast_pow(int base, int exp);
// clang-format on

struct BinomCoe
{
    vector<int> fact, inv_fact;
    void init(int max_n)
    {
        fact.resize(max_n + 1), inv_fact.resize(max_n + 1);
        fact[0] = 1, inv_fact[0] = 1;
        FOR (n, 1, max_n) {
            fact[n]     = mul(fact[n - 1], n);
            inv_fact[n] = fast_pow(fact[n], MOD - 2);
        }
    }
    int operator()(const int& n, const int& k)
    {
        return mul(fact[n], mul(inv_fact[n - k], inv_fact[k]));
    }
};


// Global variables
int nverts, nedges;
vector<bool> seq;
vector<vector<int>> adj;
vector<pair<int, int>> dq;
vector<bool> visited, colour;
BinomCoe binom;


void input();
int calc_score(const int& start);


int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    input();

    // Iterate over each connected component
    int score = 1;
    visited.resize(nverts + 1);
    colour.resize(nverts + 1);
    FOR (start, 1, nverts) {
        if (visited[start]) continue;
        score = mul(score, calc_score(start));
    }

    cout << score << "\n";
    return 0;
}

int
calc_score(const int& start)
{
    bool odd_cycle = false;
    array<int, 2> cnt{0, 0}, popcnt{0, 0};
    visited[start] = true;
    dq.emplace_back(start, 0);
    while (!dq.empty()) {
        auto [v, p] = std::move(dq.back());
        dq.pop_back();
        cnt[colour[v]] += 1;
        popcnt[colour[v]] += seq[v];
        for (const auto& neigh : adj[v]) {
            if (neigh == p) continue;
            if (visited[neigh]) {  // cycle
                odd_cycle |= (colour[neigh] == colour[v]);
                continue;
            }
            colour[neigh]  = !colour[v];
            visited[neigh] = true;
            dq.emplace_back(neigh, v);
        }
    }
    debug(start, odd_cycle, cnt, popcnt);
    if (cnt[0] + cnt[1] <= 1) return 1;

    // Case 1: we can reach all masks with this parity (thanks to an odd_cycle)
    if (odd_cycle) return fast_pow(2, cnt[0] + cnt[1] - 1);

    // Case 2: we split into two and calculate perms assuming popcnt
    int score = 0;
    if (popcnt[0] > popcnt[1]) swap(popcnt[0], popcnt[1]), swap(cnt[0], cnt[1]);
    for (int pc0 = 0, pc1 = popcnt[1] - popcnt[0];
         pc0 <= cnt[0] && pc1 <= cnt[1];
         ++pc0, ++pc1)
        score = add(score, mul(binom(cnt[0], pc0), binom(cnt[1], pc1)));
    return score;
}

void
input()
{
    cin >> nverts >> nedges;
    binom.init(nverts);
    seq.resize(nverts + 1);
    FOR (v, 1, nverts) {
        bool b;
        cin >> b;
        if (b) seq[v].flip();
    }
    adj.resize(nverts + 1);
    REP (e, nedges) {
        int a, b;
        cin >> a >> b;
        adj[a].emplace_back(b);
        adj[b].emplace_back(a);
    }
}

int
fast_pow(int base, int exp)
{
    int res = 1;
    while (exp) {
        if (exp & 1) res = mul(res, base);
        base = mul(base, base);
        exp >>= 1;
    }
    return res;
}