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>
#pragma GCC optimize("O2,unroll-loops")
using namespace std;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG = 20;
int n, m, k, u, v, x, y, a, b, ans;
int A[2][MAXN], dp[2][MAXN];
int lcp0[MAXN], lcp1[MAXN], lcp2[MAXN], sz;
string s, t, S[2];
void Build() {
  sz = max(s.size(), t.size());
  s.resize(sz + 1, '#');
  t.resize(sz + 1, '#');
  lcp0[sz] = lcp1[sz] = lcp2[sz] = 0;
  for (int i = sz - 1; ~i; i--) {
    if (s[i] == t[i])
      lcp0[i] = lcp0[i + 1] + 1;
    else
      lcp0[i] = 0;
  }
  lcp1[sz - 1] = 0;
  for (int i = sz - 2; ~i; i--) {
    if (s[i] == t[i + 1])
      lcp1[i] = lcp1[i + 1] + 1;
    else
      lcp1[i] = 0;
  }
  for (int i = sz - 1; i; i--) {
    if (s[i] == t[i - 1])
      lcp2[i] = lcp2[i + 1] + 1;
    else
      lcp2[i] = 0;
  }
}
bool cmp(int x, int y) {
  if (x == -1 && y == -1) return (lcp0[0] < sz && s[lcp0[0]] < t[lcp0[0]]);
  if (x == -1) {
    if (lcp0[0] < y) return s[lcp0[0]] < t[lcp0[0]];
    return s[lcp1[y] + y] < t[lcp1[y] + y + 1];
  }
  if (y == -1) {
    if (lcp0[0] < x) return s[lcp0[0]] < t[lcp0[0]];
    return s[lcp2[x + 1] + x + 1] < t[lcp2[x + 1] + x];
  }
  int mn = min(x, y), mx = max(x, y);
  if (lcp0[0] < mn) return s[lcp0[0]] < t[lcp0[0]];
  if (x <= y && lcp2[x + 1] < y - x)
    return s[lcp2[x + 1] + x + 1] < t[lcp2[x + 1] + x];
  if (x > y && lcp1[y] < x - y) return s[lcp1[y] + y] < t[lcp1[y] + y + 1];
  return s[lcp0[mx + 1] + mx + 1] < t[lcp0[mx + 1] + mx + 1];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  A[0][0] = -1;
  dp[0][0] = 1;
  for (int shit = 1; shit <= n; shit++) {
    int x = (shit & 1);
    cin >> S[x];
    s = t = S[x];
    Build();
    for (int i = 0; i <= sz; i++) A[x][i] = i - 1;
    sort(A[x], A[x] + sz + 1, cmp);
    s = S[x];
    t = S[x ^ 1];
    Build();
    int j = 0;
    long long sum = 0;
    for (int i = 0; i <= sz; i++) {
      while (j <= S[x ^ 1].size() && !cmp(A[x][i], A[x ^ 1][j]))
        sum = (sum + dp[x ^ 1][j++]) % mod;
      dp[x][i] = sum;
    }
  }
  for (int i = 0; i <= S[n & 1].size(); i++) ans = (ans + dp[n & 1][i]) % mod;
  cout << ans << "\n";
  return 0;
}