Problem: This is a harder version of the problem E with larger constraints.

Twilight Sparkle has received a new task from Princess Celestia. This time she asked to decipher the ancient scroll containing important knowledge of pony origin.

To hide the crucial information from evil eyes, pony elders cast a spell on the scroll. That spell adds exactly one letter in any place to each word it is cast on. To make the path to the knowledge more tangled elders chose some of words in the scroll and cast a spell on them.

Twilight Sparkle knows that the elders admired the order in all things so the scroll original scroll contained words in lexicographically non-decreasing order. She is asked to delete one letter from some of the words of the scroll (to undo the spell) to get some version of the original scroll. 

Unfortunately, there may be more than one way to recover the ancient scroll. To not let the important knowledge slip by Twilight has to look through all variants of the original scroll and find the required one. To estimate the maximum time Twilight may spend on the work she needs to know the number of variants she has to look through. She asks you to find that number! Since that number can be very big, Twilight asks you to find it modulo 10^9+7.

It may occur that princess Celestia has sent a wrong scroll so the answer may not exist.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

  * a is a prefix of b, but a ≠ b;
  * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5): the number of words in the scroll.

The i-th of the next n lines contains a string consisting of lowercase English letters: the i-th word in the scroll. The length of each word is at least one. The sum of lengths of words does not exceed 10^6.

Output

Print one integer: the number of ways to get a version of the original from the scroll modulo 10^9+7.

Examples

Input


3
abcd
zaza
ataka


Output


4


Input


4
dfs
bfs
sms
mms


Output


8


Input


3
abc
bcd
a


Output


0


Input


6
lapochka
kartyshka
bigbabytape
morgenshtern
ssshhhiiittt
queen


Output


2028

Note

Notice that the elders could have written an empty word (but they surely cast a spell on it so it holds a length 1 now).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const unsigned long long base = 233;
const int mod = 1e9 + 7;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
unsigned long long pw[1000100];
int n, len[100010], ans;
string s[100010];
vector<int> p[100010], dp[100010];
vector<unsigned long long> h[100010];
inline unsigned long long Hash(vector<unsigned long long> &h, int l, int r) {
  return h[r] - (l == 0 ? 0 : h[l - 1]) * pw[r - l + 1];
}
inline bool same(int i, int l1, int r1, int j, int l2, int r2) {
  return Hash(h[i], l1, r1) == Hash(h[j], l2, r2);
}
int cmp(int i, int l1, int r1, int j, int l2, int r2) {
  int l = 0, r = min(r1 - l1 + 1, r2 - l2 + 1) + 1, p = 0;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (same(i, l1, l1 + mid - 1, j, l2, l2 + mid - 1)) {
      p = mid, l = mid + 1;
    } else {
      r = mid;
    }
  }
  char ci = (r1 <= l1 + p - 1 ? char(0) : s[i][l1 + p]);
  char cj = (r2 <= l2 + p - 1 ? char(0) : s[j][l2 + p]);
  if (ci == cj) return 0;
  if (ci < cj) return 1;
  if (ci > cj) return -1;
}
inline bool Comp(int i, int x, int j, int y) {
  int p1 = min(x, y), p2 = max(x, y);
  int t = cmp(i, 0, p1 - 1, j, 0, p1 - 1);
  if (t == 1) return true;
  if (t == -1) return false;
  t = (x <= y
           ? cmp(i, x + 1, min(len[i] - 1, y), j, x, min(len[j] - 1, y - 1))
           : cmp(i, y, min(len[i] - 1, x - 1), j, y + 1, min(len[j] - 1, x)));
  if (t == 1) return true;
  if (t == -1) return false;
  t = cmp(i, p2 + (x < len[i] - 1), len[i] - 1, j, p2 + (y < len[j] - 1),
          len[j] - 1);
  if (t == 1) return true;
  if (t == -1) return false;
  return true;
}
int main() {
  n = read();
  pw[0] = 1;
  for (int i = 1; i <= 1000000; ++i) {
    pw[i] = pw[i - 1] * base;
  }
  for (int i = 1; i <= n; ++i) {
    cin >> s[i];
    s[i] += '#';
    len[i] = s[i].length();
    p[i].resize(len[i]);
    h[i].resize(len[i]);
    dp[i].resize(len[i]);
  }
  for (int i = 1; i <= n; ++i) {
    h[i][0] = s[i][0];
    for (int j = 1; j < len[i]; ++j) {
      h[i][j] = h[i][j - 1] * base + s[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < len[i]; ++j) {
      p[i][j] = j;
    }
    static int nxt[1000100];
    nxt[len[i] - 1] = len[i];
    for (int j = len[i] - 2; j >= 0; --j) {
      nxt[j] = (s[i][j] == s[i][j + 1] ? nxt[j + 1] : j + 1);
    }
    sort(p[i].begin(), p[i].end(), [&](int x, int y) {
      return nxt[x] == nxt[y]
                 ? x < y
                 : (x < y ? s[i][x] > s[i][nxt[x]] : s[i][y] < s[i][nxt[y]]);
    });
  }
  for (int j = 0; j < len[1]; ++j) {
    dp[1][j] = 1;
  }
  for (int i = 2; i <= n; ++i) {
    int k = 0, sum = 0;
    for (int j = 0; j < len[i]; ++j) {
      while (k < len[i - 1] && Comp(i - 1, p[i - 1][k], i, p[i][j]))
        sum = (sum + dp[i - 1][p[i - 1][k]]) % mod, ++k;
      dp[i][p[i][j]] = sum;
    }
  }
  for (int j = 0; j < len[n]; ++j) {
    ans = (ans + dp[n][j]) % mod;
  }
  printf("%d\n", ans);
  return 0;
}