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
//Jakub Nowak XIV LO Wroclaw
#include<bits/stdc++.h>
using namespace std;

#define boost ios_base::sync_with_stdio(false); cin.tie(0);
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define st first
#define nd second
const int inf = (int)(1e18)+7;
const int mod = 1e9 + 7;

void wypisz(auto &X) {
    for(auto &x : X) cout << x << " ";
    cout << "\n";
}

void wypisz_pair(auto &X) {
    for(auto &x : X) cout << x.st << "|" << x.nd << " ";
    cout << "\n";
}

int pow(int a, int x) {
    int ans = 1;
    for(; x; x/=2, a = (a*a)%mod) {
        //cout << a%mod << " ";
        if(x%2) ans = (ans*a)%mod;
    }
    return ans;
}

int rev(int x) {
    return pow(x, mod-2);
}

struct newton{
    vi SILNIA = vi(1, 1);
    vi REV_SILNIA = vi(1, 1);

    newton(int n) {
        for(int i=1; i<=n; i++) {
            SILNIA.pb((SILNIA.back()*i)%mod);
        }
        for(int i=1; i<=n; i++) {
            REV_SILNIA.pb((REV_SILNIA.back()*rev(i))%mod);
        }
    }

    int podaj(int n, int k) {
        return (((SILNIA[n]*REV_SILNIA[k])%mod)*REV_SILNIA[n-k])%mod;
    }
};

void solve() {
    int n, m;
    vector<bool> C(1, false);
    vector<vi> V(1, vi(0));
    int ans = 1;
    //return;
    newton NEWT(1);
    
    cin >> n >> m;
    NEWT = newton(n);
    V = vector<vi>(n+1, vi(0));
    for(int i=1; i<=n; i++) {
        bool x;
        cin >> x;
        C.pb(x);
    }
    for(int i=1; i<=m; i++) {
        int a, b;
        cin >> a >> b;
        V[a].pb(b);
        V[b].pb(a);
    }

    /*
    for(auto x:C) cout << x << " ";
    cout << "\n";
    for(int i=0; i<V.size(); i++) wypisz(V[i]);

    for(int i=0; i<=5; i++) for(int j=0; j<=i; j++) cout << "(" << i << "/" << j << "): " << NEWT.podaj(i, j) << "\n";

    cout << "SILNIE:\n";
    for(int i=0; i<=5; i++) cout << NEWT.SILNIA[i] << " ";
    cout << "\nREV_SILNIE:\n";
    for(int i=0; i<=5; i++) cout << NEWT.REV_SILNIA[i] << " ";
    cout << "\n";
    cout << "REV:\n";
    for(int i=1; i<=5; i++) cout << rev(i) << " ";
    cout << "\n";
    return;
    */

    {
        vector<bool> BEEN(n+1, false);
        vector<bool> ZBIOR(n+1, false);

        for(int i=1; i<=n; i++) {
            if(BEEN[i]) continue;

            bool czy_dwudzielny = true;
            vi A(0);
            vi B(0);

            function<void(bool, int)> dfs = [&](bool zbior, int v) {
                BEEN[v] = true;
                ZBIOR[v] = zbior;
                if(zbior) B.pb(v);
                else A.pb(v);
                for(auto &u : V[v]) {
                    if(BEEN[u]) {
                        if(ZBIOR[u] == ZBIOR[v]) czy_dwudzielny = false;
                        continue;
                    }
                    dfs(!zbior, u);
                }
            };

            dfs(0, i);

            //cout << czy_dwudzielny << " " << A.size() << " " << B.size() << "\n";

            if(!czy_dwudzielny) {
                for(int i=1; i<= A.size() + B.size() -1; i++) ans = (ans*2)%mod;
            }
            else {
                int ile = 0;
                int zapalone_w_a = 0, zapalone_w_b = 0;
                for(auto &u : A) zapalone_w_a += C[u];
                for(auto &u : B) zapalone_w_b += C[u];
                int x = zapalone_w_a - zapalone_w_b;

                for(int i=0; i<=A.size(); i++) {//i to zapalone w A
                    int j = i - x;//j to zapalone w B
                    if(j < 0 || j > B.size()) continue;
                    ile = (ile + NEWT.podaj(A.size(), i)*NEWT.podaj(B.size(), j))%mod;
                }
                
                ans = (ans*ile)%mod;
            }
        }
    }

    cout << ans << "\n";
}

int32_t main() {
    boost
    //cout << rev(2) << "\n";
    solve();
}