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
155
156
157
158
159
160
#include <bitset>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;

struct Button {
  u32 a, b;
};

struct Graph {
  u32 n, m;
  vector<bool> state;
  vector<vector<u32>> edges;
};

struct TestCase {
  u32 n, m;
  vector<bool> initial_state;
  vector<Button> buttons;
};

TestCase read_test_case() {
  TestCase tc;
  cin >> tc.n >> tc.m;

  tc.initial_state.resize(tc.n);
  tc.buttons.resize(tc.m);

  for (u32 i = 0; i < tc.n; i++) {
    int x;
    cin >> x;
    tc.initial_state[i] = (x == 1);
  }

  for (auto& [a, b] : tc.buttons) {
    cin >> a >> b;
    a--;
    b--;
  }

  return tc;
}

void solve_test_case(TestCase tc);

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  solve_test_case(read_test_case());
}

u64 M = 1000000007;

vector<TestCase> get_connected(const TestCase& tc);

u64 solve_connected(const TestCase& tc);

void solve_test_case(TestCase tc) {
  auto connected = get_connected(tc);
  u64 result = 1;
  for (const auto& test_case : connected)
    result = (result * solve_connected(test_case)) % M;
  cout << result << "\n";
}

vector<u32> search(const Graph& tc, u32 source, vector<bool>& visited);

vector<TestCase> get_connected(const TestCase& tc) {
  Graph g;
  g.n = tc.n;
  g.m = tc.m;
  g.edges.resize(tc.n);
  for (const auto& [a, b] : tc.buttons) {
    g.edges[a].push_back(b);
    g.edges[b].push_back(a);
  }

  vector<TestCase> result;
  vector<bool> visited(tc.n, false);
  vector<u32> mapping(tc.n, 0);

  for (u32 i = 0; i < tc.n; i++) {
    if (visited[i]) continue;
    auto component = search(g, i, visited);

    TestCase c;
    c.n = component.size();
    c.m = 0;

    for (u32 i = 0; i < component.size(); i++) {
      c.initial_state.push_back(tc.initial_state[component[i]]);
      mapping[component[i]] = i;
    }

    for (auto x : component) {
      for (auto y : g.edges[x]) {
        if (y < x) {
          c.m++;
          c.buttons.push_back({mapping[x], mapping[y]});
        }
      }
    }
    result.push_back(c);
  }

  return result;
}

vector<u32> search(const Graph& g, u32 source, vector<bool>& visited) {
  vector<u32> result;
  queue<u32> q;
  visited[source] = true;
  q.push(source);

  while (!q.empty()) {
    auto current = q.front();
    q.pop();
    result.push_back(current);

    for (auto y : g.edges[current]) {
      if (visited[y]) continue;
      visited[y] = true;
      q.push(y);
    }
  }
  return result;
}

u64 solve_connected(const TestCase& tc) {
  u64 state = 0;
  for (u32 i = 0; i < tc.n; i++) state += tc.initial_state[i] * (1 << i);

  set<u32> visited;
  queue<u32> q;
  q.push(state);
  visited.insert(state);

  while (!q.empty()) {
    auto current = q.front();
    q.pop();

    for (auto [a, b] : tc.buttons) {
      bool sa = (current & (1 << a)) == 0;
      bool sb = (current & (1 << b)) == 0;

      if (sa != sb) continue;
      u32 next = current ^ (1 << a) ^ (1 << b);
      if (visited.count(next) == 1) continue;
      q.push(next);
      visited.insert(next);
    }
  }
  return visited.size();
}