#include #include #include namespace nono { class UnionFind { public: UnionFind() = default; UnionFind(int size): size_(size), data_(size, -1) {} int leader(int x) { if (data_[x] < 0) { return x; } else { return data_[x] = leader(data_[x]); } } bool merge(int lhs, int rhs) { if (same(lhs, rhs)) { return false; } lhs = leader(lhs); rhs = leader(rhs); if (-data_[lhs] < -data_[rhs]) { std::swap(lhs, rhs); } data_[lhs] += data_[rhs]; data_[rhs] = lhs; return true; } bool same(int lhs, int rhs) { return (leader(lhs) == leader(rhs)); } int size(int x) { return -data_[leader(x)]; } int size() const { return size_; } std::vector> groups() { std::vector leader_buf(size_), group_size(size_); for (int i = 0; i < size_; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector> result(size_); for (int i = 0; i < size_; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < size_; i++) { result[leader_buf[i]].push_back(i); } result.erase(std::remove_if(result.begin(), result.end(), [&](const std::vector& v) { return v.empty(); }), result.end()); return result; } private: int size_; std::vector data_; }; } // namespace nono #include namespace nono { namespace internal { constexpr bool is_prime(unsigned long long n) { for (unsigned long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } } // namespace internal template class Modint { public: constexpr Modint(unsigned long long value = 0): value_(value % MOD) {} constexpr Modint pow(long long exp) const { Modint result(1); Modint base(*this); while (exp > 0) { if (exp & 1) { result *= base; } base *= base; exp >>= 1; } return result; } constexpr Modint inv() const { return pow(MOD - 2); } void set(unsigned long long value) { if (value >= MOD) value %= MOD; value_ = value; } unsigned long long get() const { return value_; } constexpr Modint& operator+=(const Modint other) { this->value_ += other.value_; if (this->value_ >= MOD) this->value_ -= MOD; return *this; } constexpr Modint& operator-=(const Modint other) { this->value_ += MOD - other.value_; if (this->value_ >= MOD) this->value_ -= MOD; return *this; } constexpr Modint& operator*=(const Modint other) { this->value_ *= other.value_; if (this->value_ >= MOD) this->value_ %= MOD; return *this; } constexpr Modint& operator/=(const Modint other) { this->value_ *= other.inv(); if (this->value_ >= MOD) this->value_ %= MOD; return *this; } constexpr friend Modint operator+(const Modint lhs, const Modint rhs) { return Modint(lhs) += rhs; } constexpr friend Modint operator-(const Modint lhs, const Modint rhs) { return Modint(lhs) -= rhs; } constexpr friend Modint operator*(const Modint lhs, const Modint rhs) { return Modint(lhs) *= rhs; } constexpr friend Modint operator/(const Modint lhs, const Modint rhs) { return Modint(lhs) /= rhs; } friend std::istream& operator>>(std::istream& stream, Modint& mint) { unsigned long long value; stream >> value; mint.set(value); return stream; } friend std::ostream& operator<<(std::ostream& stream, Modint mint) { stream << mint.get(); return stream; } private: unsigned long long value_; }; } // namespace nono namespace nono { void solve() { using Mint = Modint<998244353>; int n, m; std::cin >> n >> m; std::vector a(n); for (int i = 0; i < n; i++) std::cin >> a[i]; UnionFind uf(n); for (int i = 0; i < m; i++) { int u, v; std::cin >> u >> v; u--; v--; uf.merge(u, v); } Mint ans = 1; for (auto group: uf.groups()) { Mint sum = 0; for (int v: group) { sum += a[v]; } ans *= sum.pow(group.size()); } std::cout << ans << std::endl; } } // namespace nono int main() { std::cin.tie(0)->sync_with_stdio(false); std::cout << std::fixed << std::setprecision(15); int t = 1; while (t--) nono::solve(); }