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;
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
int mod = 1e9 + 7;
int p = 31;
int smax = 1000007;
vector<int> ind1, ind2;
vector<int> nex;
void msor(string s) {
  ind2.resize(s.size() + 1);
  nex.resize(s.size());
  int L = 0, R = s.size();
  nex[s.size() - 1] = s.size();
  for (int i = s.size() - 2; i >= 0; i--) {
    if (s[i] == s[i + 1])
      nex[i] = nex[i + 1];
    else
      nex[i] = i + 1;
  }
  s.push_back('#');
  for (int i = 0; i < s.size() - 2; i++) {
    if (s[i] < s[nex[i]]) {
      ind2[R] = i;
      R--;
    } else {
      ind2[L] = i;
      L++;
    }
  }
  ind2[L] = s.size() - 2;
  ind2[L + 1] = s.size() - 1;
}
vector<unsigned long long> h1, h2;
vector<unsigned long long> stp;
string a, b;
bool cmp(int ind1, int ind2) {
  int L = -1,
      R = min(a.size() - (ind1 < a.size()), b.size() - (ind2 < b.size()));
  while (L < R - 1) {
    int m = (L + R) / 2;
    unsigned long long p1, p2;
    if (ind1 > m)
      p1 = h1[m + 1];
    else
      p1 = (h1[ind1] * stp[m + 1 - ind1]) % mod +
           (h1[m + 2] + mod - (h1[ind1 + 1] * stp[m + 1 - ind1]) % mod) % mod;
    p1 %= mod;
    if (ind2 > m)
      p2 = h2[m + 1];
    else
      p2 = (h2[ind2] * stp[m + 1 - ind2]) % mod +
           (h2[m + 2] + mod - (h2[ind2 + 1] * stp[m + 1 - ind2]) % mod) % mod;
    p2 %= mod;
    if (p1 == p2)
      L = m;
    else
      R = m;
  }
  if (ind1 == a.size()) ind1++;
  if (ind2 == b.size()) ind2++;
  if ((R >= ind1 ? R + 1 : R) > a.size()) return 1;
  if ((R >= ind2 ? R + 1 : R) > b.size()) return 0;
  return a[R >= ind1 ? R + 1 : R] <= b[R >= ind2 ? R + 1 : R];
}
void calch(string s) {
  h2.resize(s.size() + 1);
  h2[0] = 0;
  for (int i = 0; i < s.size(); i++) {
    h2[i + 1] = (h2[i] * p + (s[i] - 'a' + 1)) % mod;
  }
}
void solve() {
  int n;
  cin >> n;
  cin >> a;
  vector<unsigned long long> dp1(a.size() + 1, 1), dp2;
  msor(a);
  ind1 = ind2;
  calch(a);
  h1 = h2;
  for (int i = 1; i < n; i++) {
    cin >> b;
    msor(b);
    calch(b);
    dp2.resize(b.size() + 1);
    fill(dp2.begin(), dp2.end(), 0);
    int L = 0, R = 0;
    while (R < ind2.size() && L < ind1.size()) {
      if (cmp(ind1[L], ind2[R])) {
        dp2[R] = (dp2[R] + dp1[L]) % mod;
        L++;
      } else {
        R++;
        if (R < ind2.size()) dp2[R] = dp2[R - 1];
      }
    }
    R++;
    while (R < ind2.size()) {
      dp2[R] = dp2[R - 1];
      R++;
    }
    dp1 = dp2;
    ind1 = ind2;
    a = b;
    h1 = h2;
  }
  unsigned long long ans = 0;
  for (unsigned long long i : dp1) {
    ans = (ans + i) % mod;
  }
  cout << ans;
}
int main() {
  stp.resize(smax);
  stp[0] = 1;
  for (int i = 1; i < smax; i++) {
    stp[i] = (stp[i - 1] * p) % mod;
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}