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;
long long MOD = 1e9 + 7 + 0.5;
template <typename T>
struct SegTree {
  struct SegTreeNode {
    T val;
    T addMark;
  };
  vector<SegTreeNode> nodes;
  T *arr;
  int istart, iend, maxIndex;
  SegTree() {}
  SegTree(T *arr1, int start, int end) : arr(arr1), istart(start), iend(end) {
    nodes.resize((end - start + 1) << 2);
    maxIndex = -1;
    build(0, arr, istart, iend);
  }
  void maintain(int root) {
    int left = root * 2 + 1, right = root * 2 + 2;
    nodes[root].val = (nodes[left].val + nodes[right].val) % MOD;
  }
  void build(int root, T arr[], int istart, int iend) {
    nodes[root].addMark = 0;
    if (istart == iend) {
      if (root > maxIndex) maxIndex = root;
      nodes[root].val = arr[istart] % MOD;
    } else {
      int mid = (istart + iend) / 2;
      build(root * 2 + 1, arr, istart, mid);
      build(root * 2 + 2, arr, mid + 1, iend);
      maintain(root);
    }
  }
  void pushDown(int root) { return; }
  T query(int qstart, int qend) { return query(0, istart, iend, qstart, qend); }
  T query(int root, int nstart, int nend, int qstart, int qend) {
    if (qstart > nend || qend < nstart) {
      pushDown(root);
      return 0;
    }
    if (qstart <= nstart && qend >= nend) {
      pushDown(root);
      return nodes[root].val;
    }
    pushDown(root);
    int mid = (nstart + nend) / 2;
    return (query(root * 2 + 1, nstart, mid, qstart, qend) +
            query(root * 2 + 2, mid + 1, nend, qstart, qend)) %
           MOD;
  }
  void updateOne(int index, T addVal) {
    updateOne(0, istart, iend, index, addVal);
  }
  void updateOne(int root, int nstart, int nend, int index, T addVal) {
    if (nstart == nend) {
      if (index == nstart) {
        pushDown(root);
        nodes[root].val = (nodes[root].val + addVal) % MOD;
      }
      return;
    }
    int mid = (nstart + nend) / 2;
    pushDown(root);
    if (index <= mid)
      updateOne(root * 2 + 1, nstart, mid, index, addVal);
    else
      updateOne(root * 2 + 2, mid + 1, nend, index, addVal);
    maintain(root);
  }
};
const int V = 1e5 + 20;
struct node {
  int v;
  int idx;
  bool flag;
  node(int a = 0, int b = 0, bool c = 0) : v(a), idx(b), flag(c) {}
};
vector<node> adj[V];
int parent[V], heavy[V];
int depth[V], size[V];
int chain[V], head[V], pos[V];
int vist[V];
vector<long long> realChain[V];
SegTree<long long> trees[V];
int ids[V];
int n, m;
void DFS(int i) {
  size[i] = 1, vist[i] = 1;
  for (int k = 0; k < adj[i].size(); ++k) {
    auto &ele = adj[i][k];
    int j = ele.v;
    if (j == parent[i]) {
      ele.flag = 1;
      continue;
    }
    if (vist[j] == 1) {
      ele.flag = 0;
      continue;
    }
    ele.flag = 1;
    parent[j] = i;
    ids[j] = ele.idx;
    depth[j] = depth[i] + 1;
    DFS(j);
    size[i] += size[j];
    if (heavy[i] == -1 || size[j] > size[heavy[i]]) heavy[i] = j;
  }
}
void heavylight_DFS(int N) {
  memset(heavy, -1, sizeof(heavy));
  memset(vist, 0, sizeof(vist));
  for (int i = N; i >= 1; i--) {
    if (!vist[i]) {
      parent[i] = -1;
      depth[i] = 0;
      DFS(i);
    }
  }
  int c = 0;
  for (int i = 1; i <= N; ++i)
    if (parent[i] == -1 || heavy[parent[i]] != i) {
      for (int k = i, p = 0; k != -1; k = heavy[k]) {
        chain[k] = c, head[k] = i, pos[k] = p++;
        realChain[c].push_back(0);
      }
      c++;
    }
  for (int i = 0; i < c; i++) {
    trees[i] = SegTree<long long>(realChain[i].data(), 0,
                                  (int)realChain[i].size() - 1);
  }
}
long long query(int i, int j) {
  long long ans = 0;
  int cid, pp, pp2;
  while (chain[i] != chain[j]) {
    if (depth[head[i]] > depth[head[j]]) swap(i, j);
    cid = chain[j], pp = pos[j];
    ans = (ans + trees[cid].query(0, pp)) % MOD;
    j = parent[head[j]];
  }
  cid = chain[i], pp = pos[i], pp2 = pos[j];
  if (pp > pp2) swap(pp, pp2);
  if (pp == pp2)
    return ans;
  else {
    pp++;
    ans = (ans + trees[cid].query(pp, pp2)) % MOD;
    return ans;
  }
}
void update(int i, int j, long long addvalue) {
  int cid, pp, pp2;
  while (chain[i] != chain[j]) {
    if (depth[head[i]] > depth[head[j]]) swap(i, j);
    cid = chain[j], pp = pos[j];
    trees[cid].updateOne(pp, addvalue);
    j = parent[head[j]];
  }
  cid = chain[i], pp = pos[i], pp2 = pos[j];
  if (pp > pp2) swap(pp, pp2);
  if (pp != pp2) {
    pp++;
    assert(pp == pp2);
    trees[cid].updateOne(pp, addvalue);
  }
}
struct ins {
  int k;
  vector<pair<int, int> > det;
  vector<int> aso;
};
ins inset[V];
struct ns {
  int parent;
  int ranks;
  int root;
};
const int MAX = V;
ns ts[MAX];
void ini() {
  for (int i = 1; i <= n; i++)
    ts[i].parent = i, ts[i].ranks = 0, ts[i].root = i;
}
int findSet(int x) {
  if (ts[x].parent != x)
    return ts[x].parent = findSet(ts[x].parent);
  else
    return x;
}
void unions(int x, int y) {
  int rootX = findSet(x), rootY = findSet(y);
  if (rootX == rootY) return;
  if (ts[rootX].ranks > ts[rootY].ranks) swap(rootX, rootY);
  ts[rootX].parent = rootY;
  if (ts[rootX].ranks == ts[rootY].ranks) ts[rootY].ranks++;
}
int main() {
  scanf("%d", &n);
  ini();
  for (int i = (1); i <= (n); ++i) {
    int k;
    scanf("%d", &k);
    inset[i].k = k;
    vector<int> roots;
    for (int j = (1); j <= (k); ++j) {
      int v, x;
      scanf("%d%d", &v, &x);
      inset[i].det.push_back({v, x});
      int vroot = ts[findSet(v)].root;
      roots.push_back(vroot);
    }
    inset[i].aso = roots;
    for (int j = (0); j <= ((int)roots.size() - 1); ++j) {
      int r = roots[j];
      unions(r, i);
      adj[r].push_back(i), adj[i].push_back(r);
    }
    ts[findSet(i)].root = i;
  }
  heavylight_DFS(n);
  long long ans = 0;
  for (int i = (1); i <= (n); ++i) {
    int k = inset[i].k;
    for (int j = (0); j <= (k - 1); ++j) {
      int v = inset[i].det[j].first, x = inset[i].det[j].second;
      int vroot = inset[i].aso[j];
      long long tmp = query(v, vroot);
      tmp = (tmp + x) % MOD;
      ans = (ans + tmp) % MOD;
      update(vroot, i, tmp);
    }
  }
  printf("%lld\n", (ans + MOD) % MOD);
}