#include <iostream>
#include <vector>
#include <queue>

using namespace std;

const int MOD = 1e9 + 7;

// vals is 1-indexed so vals[1] is the value of node 1
vector<int> findOptimalShortestPaths(int n, int m, vector<int>& vals, vector<vector<int>>& edges) {
    // Build adjacency list
    vector<vector<int>> adj(n + 1);
    for (auto& edge : edges) {
        adj[edge[0]].push_back(edge[1]);
        adj[edge[1]].push_back(edge[0]);
    }

    // State arrays
    vector<int> dist(n + 1, 1e9);       // Shortest distance
    vector<int> max_fives(n + 1, -1);   // Maximum 5s on a shortest path
    vector<int> ways(n + 1, 0);         // Number of optimal paths

    // Initialize source node (Node 1)
    dist[1] = 0;
    max_fives[1] = (vals[1] == 5 ? 1 : 0);
    ways[1] = 1;

    queue<int> q;
    q.push(1);

    // Standard BFS
    while (!q.empty()) {
        int u = q.front();
        q.pop();

        for (int v : adj[u]) {
            int current_fives = max_fives[u] + (vals[v] == 5 ? 1 : 0);

            // Case 1: First time reaching node v (Strictly shorter path)
            if (dist[u] + 1 < dist[v]) {
                dist[v] = dist[u] + 1;
                max_fives[v] = current_fives;
                ways[v] = ways[u];
                q.push(v);
            } 
            // Case 2: Reaching node v via another shortest path
            else if (dist[u] + 1 == dist[v]) {
                if (current_fives > max_fives[v]) {
                    // Found a path with the same length but more 5s
                    max_fives[v] = current_fives;
                    ways[v] = ways[u];
                } else if (current_fives == max_fives[v]) {
                    // Found a path with the same length AND same number of 5s
                    ways[v] = (ways[v] + ways[u]) % MOD;
                }
            }
        }
    }

    // Prepare the final result for nodes 1 to n
    vector<int> result(n);
    for(int i = 1; i <= n; ++i) {
        result[i-1] = ways[i];
    }
    
    return result;
}

int main() {
    // Fast I/O
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    if (!(cin >> n >> m)) return 0;

    // vals is 1-indexed as required by your function
    vector<int> vals(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> vals[i];
    }

    vector<vector<int>> edges(m, vector<int>(2));
    for (int i = 0; i < m; ++i) {
        cin >> edges[i][0] >> edges[i][1];
    }

    vector<int> result = findOptimalShortestPaths(n, m, vals, edges);

    // Print the number of optimal paths to each node
    for (int i = 0; i < n; ++i) {
        cout << result[i] << (i == n - 1 ? "" : " ");
    }
    cout << "\n";

    return 0;
}