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
#include <iostream>
#include <vector>
#include <utility>
#include <memory>

namespace {
	using std::ios_base, std::cin, std::cout;
	using std::vector, std::pair, std::unique_ptr, std::make_unique;

	using bulbs_t = vector<bool>;
	using switch_t = pair<size_t, size_t>;
	using v_switches_t = vector<switch_t>;
	using v_switch_idx_t = vector<size_t>;

	constexpr size_t MOD = 1000000007;

	struct Node {
		unique_ptr<Node> zero;
		unique_ptr<Node> one;
	};
	using trie_t = unique_ptr<Node>;

	v_switches_t switches;
	trie_t trie;
	bulbs_t bulbs;

	bool insert_bulbs(trie_t const & trie, bulbs_t const & bulbs, size_t idx = 0) {
		bool result = false;
		if (trie && idx < bulbs.size()) {
			if (bulbs[idx]) {
				if (!trie->one) {
					trie->one = make_unique<Node>();
					result = true;
				}
				if (idx < bulbs.size() - 1)
					result = insert_bulbs(trie->one, bulbs, idx + 1);
			} else {
				if (!trie->zero) {
					trie->zero = make_unique<Node>();
					result = true;
				}
				if (idx < bulbs.size() - 1)
					result = insert_bulbs(trie->zero, bulbs, idx + 1);
			}
		}
		return result;
	}

	size_t configs_number(v_switch_idx_t & parents) {
		size_t num = 1;
		for (size_t idx = 0; idx < switches.size(); ++idx) {
			if (parents.size() == 0 || switches[parents.back()] != switches[idx])
				if (bulbs[switches[idx].first] == bulbs[switches[idx].second]) {
					bulbs[switches[idx].first] = !bulbs[switches[idx].first];
					bulbs[switches[idx].second] = bulbs[switches[idx].first];
					if (insert_bulbs(trie, bulbs)) {
						size_t parents_size = parents.size(), prev_parent;
						if (parents_size == 0) {
							parents.push_back(idx);
						} else {
							prev_parent = parents[0];
							parents[0] = idx;
						}
						num += configs_number(parents);
						if (num >= MOD)
							num -= MOD;
						if (parents_size == 0)
							parents.pop_back();
						else
							parents[0] = prev_parent;
					}
					bulbs[switches[idx].first] = !bulbs[switches[idx].first];
					bulbs[switches[idx].second] = bulbs[switches[idx].first];
				}
		}
		return num;
	}
};

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

	size_t n, m;
	cin >> n;
	cin >> m;
	size_t ci;
	for (size_t i = 0; i < n; ++i) {
		cin >> ci;
		if (ci == 1) bulbs.push_back(true);
		else bulbs.push_back(false);
	}
	size_t ai, bi;

	for (size_t i = 0; i < m; ++i) {
		cin >> ai;
		cin >> bi;
		switches.emplace_back(ai - 1, bi - 1);
	}

	size_t result = 1;
	v_switch_idx_t parents;
	if (n > 1) {
		trie = make_unique<Node>();
		insert_bulbs(trie, bulbs);
		result = configs_number(parents);
	}
	std::cout << result << '\n';
}