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;
int n, sum[110000], srk[110000], rk[2100000], noww, ans, l, r;
int lcp[2100000], lcp1[2100000], lcp2[2100000], nex[2100000];
long long dp[2100000], ss[2100000], val[2100000];
const int mod = 1e9 + 7;
string s[110000];
bool check(int pl1, int pl2) {
  int u = lcp[sum[noww - 1]];
  if (u < min(pl1, pl2)) return s[noww][u] <= s[noww + 1][u];
  if (pl1 < pl2) {
    u = pl1 + lcp2[sum[noww - 1] + pl1 + 1] + 1;
    if (u <= pl2) return s[noww][u] <= s[noww + 1][u - 1];
  } else {
    u = pl2 + lcp1[sum[noww - 1] + pl2];
    if (u < pl1) return s[noww][u] <= s[noww + 1][u + 1];
  }
  u = max(pl1, pl2) + 1 + lcp[sum[noww - 1] + max(pl1, pl2) + 1];
  if (u <= min(s[noww + 1].size(), s[noww].size()) - 1)
    return s[noww][u] <= s[noww + 1][u];
  return (s[noww].size() <= s[noww + 1].size() ||
          (s[noww].size() - 1 == s[noww + 1].size() &&
           pl2 == s[noww + 1].size() - 1));
}
void binary(int l, int r, int pl) {
  if (l > r) return;
  int mid = l + r >> 1;
  if (check(rk[mid], pl)) {
    ans = mid;
    binary(mid + 1, r, pl);
  } else {
    binary(l, mid - 1, pl);
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i], s[i] += '$';
    sum[i] = sum[i - 1] + s[i].size();
  }
  for (int i = 1; i < n; i++) {
    for (int j = min(s[i].size() - 1, s[i + 1].size() - 1); j >= 0; j--)
      lcp[sum[i - 1] + j] =
          (s[i][j] == s[i + 1][j] ? lcp[sum[i - 1] + j + 1] + 1 : 0);
    for (int j = min(s[i].size() - 1, s[i + 1].size() - 2); j >= 0; j--)
      lcp1[sum[i - 1] + j] =
          (s[i][j] == s[i + 1][j + 1] ? lcp1[sum[i - 1] + j + 1] + 1 : 0);
    for (int j = min(s[i].size() - 1, s[i + 1].size()); j >= 1; j--)
      lcp2[sum[i - 1] + j] =
          (s[i][j] == s[i + 1][j - 1] ? lcp2[sum[i - 1] + j + 1] + 1 : 0);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = s[i].size() - 2; j >= 0; j--)
      nex[sum[i - 1] + j] =
          (s[i][j] == s[i][j + 1] ? nex[sum[i - 1] + j + 1] : j + 1);
    nex[sum[i] - 1] = s[i].size();
  }
  for (int i = 1; i <= n; i++) {
    srk[i] = srk[i - 1];
    for (int j = 0; j < s[i].size(); j = nex[sum[i - 1] + j]) srk[i]++;
    l = srk[i - 1] + 1, r = srk[i];
    for (int j = 0; j < s[i].size(); j = nex[sum[i - 1] + j]) {
      if (s[i][j] < s[i][nex[sum[i - 1] + j]]) {
        rk[r] = nex[sum[i - 1] + j] - 1;
        val[r] = nex[sum[i - 1] + j] - j;
        r--;
      } else {
        rk[l] = nex[sum[i - 1] + j] - 1;
        val[l] = nex[sum[i - 1] + j] - j;
        l++;
      }
    }
  }
  for (int i = 1; i <= srk[1]; i++) ss[i] = ss[i - 1] + val[i];
  for (int i = 2; i <= n; i++) {
    noww = i - 1;
    for (int j = srk[i - 1] + 1; j <= srk[i]; j++) {
      ans = -1;
      binary(srk[i - 2] + 1, srk[i - 1], rk[j]);
      if (~ans) dp[j] = val[j] * ss[ans] % mod;
    }
    ss[srk[i - 1] + 1] = dp[srk[i - 1] + 1];
    for (int j = srk[i - 1] + 2; j <= srk[i]; j++)
      ss[j] = (dp[j] + ss[j - 1]) % mod;
  }
  printf("%lld", ss[srk[n]]);
  return 0;
}