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;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const int MOD = (int)1e9 + 7;
int add(int x, int y) {
  x += y;
  if (x >= MOD) return x - MOD;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0) return x + MOD;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % MOD; }
const int N = (int)2e6 + 77;
int nxtDiff[3][N];
int pref[N], prefGood[N];
int ndp[N], nAdd[N], nAddGood[N];
vector<pii> getPositions(const vector<int> &s) {
  vector<pii> res = {{N, 1}};
  int cnt = 0;
  for (int i = 0; i < (int)s.size() - 1; i++) {
    cnt++;
    if (s[i + 1] != s[i]) {
      res.push_back({i, cnt});
      cnt = 0;
    }
  }
  return res;
}
void precalcDiff(const vector<int> &s, const vector<int> &p) {
  int n = (int)s.size(), m = (int)p.size();
  for (int d = 0; d < 3; d++) {
    int z = min(n, m + d - 1);
    nxtDiff[d][z] = z;
    for (int i = z - 1; i >= max(0, d - 1); i--) {
      if (s[i] == p[i - (d - 1)])
        nxtDiff[d][i] = nxtDiff[d][i + 1];
      else
        nxtDiff[d][i] = i;
    }
  }
}
bool cmp(int p, int q, const vector<int> &s, const vector<int> &t) {
  int z = nxtDiff[1][0];
  if (z < p && z < q) return s[z] < t[z];
  if (p < q) {
    z = nxtDiff[2][p + 1];
    if (z - 1 < q) return s[z] < t[z - 1];
    z = nxtDiff[1][q + 1];
    return s[z] < t[z];
  } else {
    z = nxtDiff[0][q];
    if (z < p) return s[z] < t[z + 1];
    z = nxtDiff[1][p + 1];
    return s[z] < t[z];
  }
}
vector<int> solve(const vector<int> &s, const vector<int> &p, vector<pii> a,
                  vector<pii> b, vector<int> dp) {
  precalcDiff(s, p);
  int n = (int)a.size(), m = (int)b.size();
  for (int i = 0; i <= m; i++) {
    ndp[i] = nAdd[i] = nAddGood[i] = 0;
  }
  for (int i = 0; i <= n; i++) {
    pref[i] = prefGood[i] = 0;
  }
  for (int i = 1; i < n; i++) {
    int x = mult(dp[i], a[i].second);
    pref[i + 1] = add(pref[i], x);
    prefGood[i + 1] = prefGood[i];
    int z = nxtDiff[1][a[i].first + 1];
    if (0 <= z && z < (int)s.size() && z < (int)p.size() && s[z] < p[z])
      prefGood[i + 1] = add(prefGood[i + 1], x);
  }
  for (int i = 0; i < n; i++)
    if (cmp(a[i].first, b[0].first, s, p))
      ndp[0] = add(ndp[0], mult(dp[i], a[i].second));
  for (int j = 1; j < m; j++)
    if (cmp(a[0].first, b[j].first, s, p))
      ndp[j] = add(ndp[j], mult(dp[0], a[0].second));
  for (int i = 1; i < n; i++) {
    int pp = a[i].first;
    int w = mult(dp[i], a[i].second);
    int z = nxtDiff[1][0];
    int q1 =
        lower_bound(b.begin() + 1, b.end(), make_pair(pp + 1, -1)) - b.begin();
    if (z < pp) {
      if (s[z] > p[z]) continue;
      nAdd[q1] = add(nAdd[q1], w);
      nAdd[m] = sub(nAdd[m], w);
      continue;
    }
    z = nxtDiff[2][pp + 1];
    int q2 = lower_bound(b.begin() + 1, b.end(), make_pair(z, -1)) - b.begin();
    if (s[z] < p[z - 1]) {
      nAdd[q2] = add(nAdd[q2], w);
      nAdd[m] = sub(nAdd[m], w);
    }
    nAddGood[q1] = add(nAddGood[q1], w);
    nAddGood[q2] = sub(nAddGood[q2], w);
  }
  for (int j = 1; j < m; j++) {
    int qq = b[j].first;
    int z = nxtDiff[1][0];
    int p1 = lower_bound(a.begin() + 1, a.end(), make_pair(qq, -1)) - a.begin();
    if (z < qq) {
      if (s[z] > p[z]) continue;
      ndp[j] = add(ndp[j], sub(pref[n], pref[p1]));
      continue;
    }
    z = nxtDiff[0][qq];
    int p2 =
        lower_bound(a.begin() + 1, a.end(), make_pair(z + 1, -1)) - a.begin();
    if (s[z] < p[z + 1]) {
      ndp[j] = add(ndp[j], sub(pref[n], pref[p2]));
    }
    ndp[j] = add(ndp[j], sub(prefGood[p2], prefGood[p1]));
  }
  for (int i = 2; i <= m; i++) {
    nAdd[i] = add(nAdd[i], nAdd[i - 1]);
    nAddGood[i] = add(nAddGood[i], nAddGood[i - 1]);
  }
  for (int i = 1; i < m; i++) {
    ndp[i] = add(ndp[i], nAdd[i]);
    int z = nxtDiff[1][b[i].first + 1];
    if (0 <= z && z < (int)s.size() && z < (int)p.size() && s[z] < p[z])
      ndp[i] = add(ndp[i], nAddGood[i]);
  }
  dp = vector<int>((int)b.size());
  for (int i = 0; i < (int)b.size(); i++) dp[i] = ndp[i];
  return dp;
}
vector<int> a[N];
int main() {
  startTime = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (char c : s) a[i].push_back(c + N);
    a[i].push_back(i);
  }
  vector<pii> old = getPositions(a[0]);
  vector<int> dp((int)old.size(), 1);
  for (int i = 1; i < n; i++) {
    vector<pii> nw = getPositions(a[i]);
    dp = solve(a[i - 1], a[i], old, nw, dp);
    old = nw;
  }
  int ans = 0;
  for (int i = 0; i < (int)old.size(); i++)
    ans = add(ans, mult(dp[i], old[i].second));
  printf("%d\n", ans);
  return 0;
}