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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = (int)1e5 + 10;
const int MOD = (int)1e9 + 7;
bool is_pr(int x) {
  for (int j = 2; j * j <= x; j++)
    if (x % j == 0) return true;
  return false;
}
int gen_mod() {
  int mod = ((int)rng() % (int)1e9) + 1249142915;
  while (is_pr(mod)) mod++;
  return mod;
}
const int HMOD = (int)1e9 + 9;
const int BASE = 27;
int powr(int n, int k) {
  if (k == 0) return 1;
  int p = powr(n, k / 2);
  p = (p * 1ll * p) % HMOD;
  if (k % 2 == 1) p = (p * 1ll * n) % HMOD;
  return p;
}
const int INV = powr(BASE, HMOD - 2);
const int L = (int)1e6 + 10;
int pwr[L];
vector<int> pf[N];
vector<int> dp[N];
string st[N];
int calc(int i, int cut, int len) {
  if (len <= cut) return pf[i][len - 1];
  int h1 = 0;
  if (cut > 0) h1 = pf[i][cut - 1];
  int h2 = (pf[i][len] - pf[i][cut] + HMOD) % HMOD;
  h2 = (h2 * 1ll * INV) % HMOD;
  return (h1 + h2) % HMOD;
}
char get(int i1, int cut, int idx) {
  if (idx < cut) {
    return st[i1][idx];
  }
  idx++;
  if (idx < st[i1].size()) {
    return st[i1][idx];
  } else {
    return '#';
  }
}
int comp(int i1, int c1, int i2, int c2) {
  int l1 = pf[i1].size();
  if (c1 < l1) l1--;
  int l2 = pf[i2].size();
  if (c2 < l2) l2--;
  int cp = 0;
  int nx;
  for (int lg = 20; lg >= 0; lg--) {
    nx = cp + (1 << lg);
    if (nx > l1 || nx > l2) continue;
    if (calc(i1, c1, nx) == calc(i2, c2, nx)) cp = nx;
  }
  char f1 = get(i1, c1, cp);
  char f2 = get(i2, c2, cp);
  if (f1 == '#') return true;
  if (f2 == '#') return false;
  return f1 <= f2;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  pwr[0] = 1ll;
  for (int i = 1; i < L; i++) pwr[i] = (pwr[i - 1] * 1ll * BASE) % HMOD;
  int n;
  cin >> n;
  vector<int> oo[n];
  int k;
  long long sum = 0;
  long long pp = 0;
  int sol = 0, p = 0;
  for (int i = 0; i < n; i++) {
    cin >> st[i];
    k = st[i].size();
    pf[i].resize(k);
    sum = 0;
    pp = 1;
    for (int j = 0; j < k; j++) {
      sum = (sum + ((st[i][j] - 'a' + 1) * 1ll * pp) % HMOD) % HMOD;
      pp = (pp * 1ll * BASE) % HMOD;
      pf[i][j] = sum;
    }
    vector<int> ord(st[i].size());
    int pa = 0, pb = st[i].size() - 1;
    int nx = 0;
    for (int j = 0; j < st[i].size(); j++) {
      while (nx < st[i].size()) {
        if (st[i][nx] == st[i][j])
          nx++;
        else
          break;
      }
      if (nx == st[i].size())
        ord[pa++] = j;
      else {
        if (st[i][j] < st[i][nx]) {
          ord[pb--] = j;
        } else {
          ord[pa++] = j;
        }
      }
    }
    for (auto x : ord) {
      oo[i].push_back(x);
      if (x == (int)st[i].size() - 1) oo[i].push_back(st[i].size());
    }
    if (i == 0) {
      dp[i].resize(oo[i].size(), 1);
    } else {
      dp[i].resize(oo[i].size());
      sol = 0, p = 0;
      for (int j = 0; j < oo[i].size(); j++) {
        while (p < oo[i - 1].size() && comp(i - 1, oo[i - 1][p], i, oo[i][j])) {
          sol += dp[i - 1][p];
          sol %= MOD;
          p++;
        }
        dp[i][j] = sol;
      }
    }
  }
  int outp = 0;
  for (auto x : dp[n - 1]) outp = (outp + x) % MOD;
  cout << outp << "\n";
  return 0;
}