Problem: An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes.

The root of vertex v of an oriented weighted forest is a vertex from which no edge goes and which can be reached from vertex v moving along the edges of the weighted oriented forest. We denote the root of vertex v as root(v).

The depth of vertex v is the sum of weights of paths passing from the vertex v to its root. Let's denote the depth of the vertex v as depth(v).

Let's consider the process of constructing a weighted directed forest. Initially, the forest does not contain vertices. Vertices are added sequentially one by one. Overall, there are n performed operations of adding. The i-th (i > 0) adding operation is described by a set of numbers (k, v1, x1, v2, x2, ... , vk, xk) and means that we should add vertex number i and k edges to the graph: an edge from vertex root(v1) to vertex i with weight depth(v1) + x1, an edge from vertex root(v2) to vertex i with weight depth(v2) + x2 and so on. If k = 0, then only vertex i is added to the graph, there are no added edges.

Your task is like this: given the operations of adding vertices, calculate the sum of the weights of all edges of the forest, resulting after the application of all defined operations, modulo 1000000007 (109 + 7).

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of operations of adding a vertex.

Next n lines contain descriptions of the operations, the i-th line contains the description of the operation of adding the i-th vertex in the following format: the first number of a line is an integer k (0 ≤ k ≤ i - 1), then follow 2k space-separated integers: v1, x1, v2, x2, ... , vk, xk (1 ≤ vj ≤ i - 1, |xj| ≤ 109). 

The operations are given in the order, in which they should be applied to the graph. It is guaranteed that sum k of all operations does not exceed 105, also that applying operations of adding vertexes does not result in loops and multiple edges. 

Output

Print a single number — the sum of weights of all edges of the resulting graph modulo 1000000007 (109 + 7).

Examples

Input

6
0
0
1 2 1
2 1 5 2 2
1 1 2
1 3 4


Output

30


Input

5
0
1 1 5
0
0
2 3 1 4 3


Output

9

Note

Conside the first sample:

  1. Vertex 1 is added. k = 0, thus no edges are added.
  2. Vertex 2 is added. k = 0, thus no edges are added.
  3. Vertex 3 is added. k = 1. v1 = 2, x1 = 1. Edge from vertex root(2) = 2 to vertex 3 with weight depth(2) + x1 = 0 + 1 = 1 is added. 
  4. Vertex 4 is added. k = 2. v1 = 1, x1 = 5. Edge from vertex root(1) = 1 to vertex 4 with weight depth(1) + x1 = 0 + 5 = 5 is added. v2 = 2, x2 = 2. Edge from vertex root(2) = 3 to vertex 4 with weight depth(2) + x1 = 1 + 2 = 3 is added.
  5. Vertex 5 is added. k = 1. v1 = 1, x1 = 2. Edge from vertex root(1) = 4 to vertex 5 with weight depth(1) + x1 = 5 + 2 = 7 is added.
  6. Vertex 6 is added. k = 1. v1 = 3, x1 = 4. Edge from vertex root(3) = 5 to vertex 6 with weight depth(3) + x1 = 10 + 4 = 14 is added.



The resulting graph is shown on the pictore below: <image>

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
bool trace;
bool is_root[100004];
int roots[100004];
class DisjointSet {
  size_t _n;
  size_t _size;

 public:
  vector<int> parent;
  vector<size_t> rank;
  DisjointSet(size_t n);
  void reset();
  void make_set(int u);
  size_t size() const;
  int find_rep(int u);
  bool union_rep(int u, int v);
};
DisjointSet::DisjointSet(size_t n)
    : _n(n), _size(n), parent(n, -1), rank(n, 0) {}
void DisjointSet::reset() {
  parent.assign(_n, -1);
  rank.assign(_n, 0);
  _size = _n;
}
void DisjointSet::make_set(int u) {
  parent[u] = -1;
  rank[u] = 0;
}
inline size_t DisjointSet::size() const { return _size; }
int DisjointSet::find_rep(int u) {
  return parent[u] < 0 ? u : parent[u] = find_rep(parent[u]);
}
bool DisjointSet::union_rep(int u, int v) {
  int u_root = find_rep(u);
  int v_root = find_rep(v);
  if (u_root == v_root) return false;
  if (rank[u_root] > rank[v_root])
    parent[v_root] = u_root;
  else {
    parent[u_root] = v_root;
    if (rank[u_root] == rank[v_root]) rank[v_root] = rank[u_root] + 1;
  }
  --_size;
  return true;
}
struct SegmentTreeNode {
  long long sum;
  SegmentTreeNode() : sum(0) {}
};
class SegmentTree {
  int N;
  void _update(int p, int sum, int node, int L, int R);
  int _count(int p, int q, int node, int L, int R);

 public:
  vector<SegmentTreeNode> tree;
  SegmentTree(int _N) : N(_N) {
    int M =
        N > 1 ? 2 * (1 << ((sizeof(int) * 8 - 1 - __builtin_clz((N - 1))) + 1))
              : 2;
    tree = vector<SegmentTreeNode>(M);
  }
  void update(int p, int sum) { _update(p, sum, 1, 0, N - 1); }
  int count(int p, int q) { return _count(p, q, 1, 0, N - 1); }
};
void SegmentTree::_update(int p, int sum, int node, int L, int R) {
  if (p < L || p > R) return;
  if (L == R) {
    tree[node].sum += sum;
    tree[node].sum %= MOD;
    return;
  }
  int cL = node * 2;
  int cR = node * 2 + 1;
  _update(p, sum, cL, L, (L + R) / 2);
  _update(p, sum, cR, (L + R) / 2 + 1, R);
  tree[node].sum = (tree[cL].sum + tree[cR].sum) % MOD;
}
int SegmentTree::_count(int p, int q, int node, int L, int R) {
  if (q < L || p > R) return 0;
  if (p <= L && R <= q) return tree[node].sum;
  int cL = node * 2;
  int cR = node * 2 + 1;
  return (_count(p, q, cL, L, (L + R) / 2) +
          _count(p, q, cR, (L + R) / 2 + 1, R)) %
         MOD;
}
vector<SegmentTree> segm_trees;
class TreeDecomposition {
  int root;
  vector<int> vis;
  vector<int> par;
  vector<int> Td, Tf;
  vector<int> subtree_size;
  vector<int> prefix_walk;
  void _dfs();

 public:
  vector<vector<int> > adj;
  vector<vector<int> > chains;
  vector<pair<int, int> > node_chain_pos;
  TreeDecomposition(int _N) : adj(_N) {}
  void decompose();
  bool is_ancestor(int u, int v) const;
};
void TreeDecomposition::_dfs() {
  int N = adj.size();
  vis = vector<int>(N);
  par = vector<int>(N, -1);
  Td = vector<int>(N);
  Tf = vector<int>(N);
  subtree_size = vector<int>(N);
  for (int u = 0; u < N; ++u) {
    if (!is_root[u]) continue;
    root = u;
    stack<pair<int, int> > S;
    S.push(pair<int, int>(root, 0));
    par[root] = root;
    for (int t = 1; !S.empty(); ++t) {
      int u = S.top().first, e = S.top().second;
      if (e == 0) {
        vis[u] = 2;
        Td[u] = t;
      }
      if (e == int(adj[u].size())) {
        Tf[u] = t;
        subtree_size[u] = 1;
        for (int j = 0; j < int(adj[u].size()); ++j) {
          int v = adj[u][j];
          if (v == par[u]) continue;
          subtree_size[u] += subtree_size[v];
        }
        prefix_walk.push_back(u);
        S.pop();
      } else {
        ++S.top().second;
        int v = adj[u][e];
        if (!vis[v]) {
          vis[v] = 1;
          par[v] = u;
          S.push(pair<int, int>(v, 0));
        }
      }
    }
  }
}
void TreeDecomposition::decompose() {
  _dfs();
  int N = adj.size();
  vector<bool> par_edge_processed(N);
  par_edge_processed[root] = true;
  for (int k = 0; k < (int)prefix_walk.size(); ++k) {
    int v = prefix_walk[k];
    if (par_edge_processed[v]) continue;
    vector<int> cur_path;
    cur_path.push_back(v);
    do {
      int u = par[v];
      cur_path.push_back(u);
      par_edge_processed[v] = true;
      if (2 * subtree_size[v] < subtree_size[u]) break;
      v = u;
    } while (!par_edge_processed[v]);
    chains.push_back(cur_path);
  }
  node_chain_pos = vector<pair<int, int> >(N);
  node_chain_pos[0].first = -1;
  for (int i = 0; i < (int)chains.size(); ++i) {
    for (int j = 0; j < int(chains[i].size()) - 1; ++j) {
      int u = chains[i][j];
      node_chain_pos[u] = pair<int, int>(i, j);
    }
  }
}
inline bool TreeDecomposition::is_ancestor(int u, int v) const {
  return Td[u] <= Td[v] && Tf[v] <= Tf[u];
}
void update(const TreeDecomposition& td, int u, int v, int sum) {
  while (u != v) {
    if (td.is_ancestor(u, v)) swap(u, v);
    int p = td.node_chain_pos[u].first;
    int lo = td.node_chain_pos[u].second, hi = int(td.chains[p].size()) - 1;
    if (td.is_ancestor(td.chains[p][hi], v)) {
      while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        if (td.is_ancestor(td.chains[p][mid], v))
          hi = mid;
        else
          lo = mid + 1;
      }
      hi = lo;
      lo = td.node_chain_pos[u].second;
    }
    segm_trees[p].update(lo, sum);
    u = td.chains[p][hi];
  }
}
long long query(const TreeDecomposition& td, int u, int v) {
  long long res = 0;
  while (u != v) {
    if (td.is_ancestor(u, v)) swap(u, v);
    int p = td.node_chain_pos[u].first;
    int lo = td.node_chain_pos[u].second, hi = int(td.chains[p].size()) - 1;
    if (td.is_ancestor(td.chains[p][hi], v)) {
      while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        if (td.is_ancestor(td.chains[p][mid], v))
          hi = mid;
        else
          lo = mid + 1;
      }
      hi = lo;
      lo = td.node_chain_pos[u].second;
    }
    res += segm_trees[p].count(lo, hi - 1);
    res %= MOD;
    u = td.chains[p][hi];
  }
  return res;
}
struct Road {
  int u, v;
};
Road roads[100004];
struct E {
  int v, root, x;
  E(int _v, int _r, int _x) : v(_v), root(_r), x(_x) {}
};
vector<E> edges[100004];
int main(int argc, char* argv[]) {
  cin.sync_with_stdio(false);
  int N;
  cin >> N;
  for (int u = 0; u < N; ++u) {
    roots[u] = u;
    is_root[u] = true;
  }
  DisjointSet dset(N);
  TreeDecomposition td(N);
  for (int u = 0; u < N; ++u) {
    int k;
    cin >> k;
    for (int kk = 0; kk < k; ++kk) {
      int v, x;
      cin >> v >> x;
      --v;
      int root_v = roots[dset.find_rep(v)];
      dset.union_rep(u, root_v);
      int rep_u = dset.find_rep(u);
      roots[rep_u] = u;
      td.adj[u].push_back(root_v);
      td.adj[root_v].push_back(u);
      is_root[root_v] = false;
      edges[u].push_back(E(v, root_v, (x + MOD) % MOD));
    }
  }
  td.decompose();
  segm_trees.clear();
  for (int i = 0; i < int(td.chains.size()); ++i)
    segm_trees.push_back(SegmentTree(int(td.chains[i].size()) - 1));
  long long res = 0;
  for (int u = 0; u < N; ++u) {
    for (int k = 0; k < edges[u].size(); ++k) {
      const E& e = edges[u][k];
      long long sum = query(td, e.v, e.root);
      long long sumx = (sum + e.x) % MOD;
      res = (res + sumx) % MOD;
      update(td, e.root, u, sumx);
    }
  }
  cout << res << endl;
  return 0;
}