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 long long MAXN = 1e5 + 5;
const long long MAXC = 1e6 + 5;
const long long hmod[2] = {(long long)1e9 + 7, (long long)1e9 + 9};
const long long ALFABET[2] = {29, 31};
const long long mod = 1e9 + 7;
long long dp[2][MAXC];
long long inverse[2];
vector<long long> pot[2];
vector<long long> H[2][MAXN];
vector<long long> nast[MAXN];
vector<long long> sorted[MAXN];
long long pom[MAXC];
string s[MAXN];
long long ans = 0;
long long fastpow(long long a, long long b, long long wsk) {
  long long wynik = 1;
  if (a == 0) return 0;
  while (b) {
    if (b & 1) wynik = (wynik * a) % hmod[wsk];
    a = (a * a) % hmod[wsk];
    b >>= 1;
  }
  return wynik;
}
long long inv(long long x, long long wsk) {
  return fastpow(x, hmod[wsk] - 2, wsk);
}
long long add(long long a, long long b) {
  a += b;
  while (a >= mod) a -= mod;
  return a;
}
long long sub(long long a, long long b) {
  a -= b;
  while (a < 0) a += mod;
  return a;
}
inline long long gethash(long long r, long long pos, long long wsk,
                         long long id) {
  if (r < pos) return (H[wsk][id][r] * pot[wsk][MAXC - 1 - r]) % hmod[wsk];
  long long haha = (long long)H[wsk][id].size() - 1;
  long long part1 = H[wsk][id][pos - 1];
  long long part2 = H[wsk][id][min(haha, r + 1)] - H[wsk][id][pos];
  while (part2 < 0) part2 += hmod[wsk];
  part2 *= inverse[wsk];
  long long Hasz = (part1 + part2) % hmod[wsk];
  Hasz = (Hasz * pot[wsk][MAXC - 1 - r]) % hmod[wsk];
  return Hasz;
}
inline char wez(long long ktora, long long pos, long long id) {
  if (ktora < pos) return s[id][ktora - 1];
  if (ktora + 1 <= (long long)s[id].size()) return s[id][ktora];
  return '0';
}
inline bool check(long long id1, long long lit1, long long id2,
                  long long lit2) {
  lit1 = sorted[id1][lit1];
  lit2 = sorted[id2][lit2];
  long long len1 = (long long)s[id1].size();
  long long len2 = (long long)s[id2].size();
  long long ip = 0, ik = min(len1, len2) + 1;
  while (ip + 1 < ik) {
    long long mid = (ip + ik) >> 1;
    if (gethash(mid, lit1, 0, id1) == gethash(mid, lit2, 0, id2) &&
        gethash(mid, lit1, 1, id1) == gethash(mid, lit2, 1, id2))
      ip = mid;
    else
      ik = mid;
  }
  return wez(ip + 1, lit1, id1) <= wez(ip + 1, lit2, id2);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < 2; i++) {
    pot[i].resize(MAXC);
    pot[i][0] = 1;
    for (long long j = 1; j <= MAXC - 1; j++) {
      pot[i][j] = (pot[i][j - 1] * ALFABET[i]) % hmod[i];
    }
  }
  inverse[0] = inv(ALFABET[0], 0);
  inverse[1] = inv(ALFABET[1], 1);
  for (long long i = 1; i <= n; i++) {
    cin >> s[i];
    for (long long j = 0; j < 2; j++) {
      H[j][i].resize((long long)s[i].size() + 1);
      H[j][i][0] = 0;
      for (long long k = 1; k <= (long long)s[i].size(); k++) {
        H[j][i][k] =
            (H[j][i][k - 1] + pot[j][k] * (s[i][k - 1] - 'a' + 1)) % hmod[j];
      }
    }
    nast[i].resize((long long)s[i].size() + 1);
    nast[i][(long long)s[i].size()] = (long long)s[i].size();
    for (long long k = (long long)s[i].size() - 1; k >= 1; k--) {
      if (s[i][k - 1] != s[i][k]) {
        nast[i][k] = k + 1;
      } else
        nast[i][k] = nast[i][k + 1];
    }
    long long l = 1, r = (long long)s[i].size();
    for (long long k = 1; k <= (long long)s[i].size(); k++) {
      if (s[i][k - 1] < s[i][nast[i][k] - 1])
        pom[r--] = k;
      else
        pom[l++] = k;
    }
    long long wsk = 0;
    sorted[i].resize((long long)s[i].size() + 2);
    for (long long k = 1; k <= (long long)s[i].size(); k++) {
      sorted[i][++wsk] = pom[k];
      if (pom[k] == (long long)s[i].size())
        sorted[i][++wsk] = (long long)s[i].size() + 1;
    }
  }
  for (long long i = 1; i <= (long long)s[1].size() + 1; i++) {
    dp[1][i] = 1;
  }
  for (long long i = 2; i <= n; i++) {
    long long sum = 0;
    long long wsk = 0;
    long long rak1 = i % 2;
    long long rak2 = (i % 2) ^ 1;
    for (long long j = 0; j < (long long)s[i].size() + 2; j++) dp[rak1][j] = 0;
    for (long long j = 1; j <= (long long)s[i].size() + 1; j++) {
      while (wsk + 1 <= (long long)s[i - 1].size() + 1 &&
             check(i - 1, wsk + 1, i, j)) {
        sum = (sum + dp[rak2][wsk + 1]) % mod;
        wsk++;
      }
      dp[rak1][j] = sum;
    }
  }
  long long rak1 = n % 2;
  for (long long i = 1; i <= (long long)s[n].size() + 1; i++)
    ans = (ans + dp[rak1][i]) % mod;
  cout << ans;
  return 0;
}