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;
long long MOD = 1e9 + 7;
long long sub(long long a, long long b, long long MOD1 = MOD) {
  long long r = a - b;
  if (r < 0) r += MOD1;
  return r;
}
long long sum(long long a, long long b, long long MOD1 = MOD) {
  long long r = a + b;
  if (r >= MOD1) r -= MOD1;
  return r;
}
struct Hash {
  __int128 MODD = 212345678987654321LL, P = 1777771, PI = 106955741089659571LL;
  vector<__int128> h, pi;
  Hash() {}
  Hash(string& s) {
    assert((P * PI) % MODD == 1);
    h.resize(s.size() + 1);
    pi.resize(s.size() + 1);
    h[0] = 0;
    pi[0] = 1;
    __int128 p = 1;
    for (int i = 1, ThxDem = s.size() + 1; i < ThxDem; ++i) {
      h[i] = (h[i - 1] + p * s[i - 1]) % MODD;
      pi[i] = (pi[i - 1] * PI) % MODD;
      p = (p * P) % MODD;
    }
  }
  long long getval(int k, int p) {
    if (p >= k) return h[k];
    __int128 v1 = h[p];
    __int128 v2 = sub(h[k + 1], h[p + 1], MODD);
    return sum(v1, v2 * PI % MODD, MODD);
  }
};
const int MAXN = 1e5 + 10;
Hash hs[MAXN];
string s[MAXN];
vector<pair<int, int> > g[MAXN];
vector<int> ac[MAXN];
bool cmp(pair<int, int> x, pair<int, int> y) {
  int top = min(int(s[x.first].size()), int(s[y.first].size())) - 1;
  int l = 1, r = top;
  while (l <= r) {
    int m = (l + r) / 2;
    long long v1 = hs[x.first].getval(m, x.second);
    long long v2 = hs[y.first].getval(m, y.second);
    if (v1 == v2)
      l = m + 1;
    else
      r = m - 1;
  }
  if (l > top) {
    int v1 = int(s[x.first].size()), v2 = int(s[y.first].size());
    if (x.second != v1 - 1) v1--;
    if (y.second != v2 - 1) v2--;
    return v1 < v2;
  }
  int id1 = l - 1, id2 = l - 1;
  if (x.second < l) id1++;
  if (y.second < l) id2++;
  return s[x.first][id1] < s[y.first][id2];
}
void order(int wh, vector<pair<int, int> >& ans) {
  int lst = -1, n = int(s[wh].size());
  vector<int> nex(n, -1);
  for (int i = n - 2; i >= 0; i--) {
    if (s[wh][i] != s[wh][i + 1])
      nex[i] = i + 1;
    else
      nex[i] = nex[i + 1];
  }
  ans.assign(n, {wh, 0});
  int le = 0, ri = n - 1;
  for (int i = 0, ThxDem = n - 1; i < ThxDem; ++i) {
    if (s[wh][i] < s[wh][nex[i]])
      ans[ri].second = i, ri--;
    else
      ans[le].second = i, le++;
  }
  ans[le].second = n - 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
    cin >> s[i];
    s[i].push_back('a' - 1);
    hs[i] = Hash(s[i]);
    order(i, g[i]);
    ac[i].assign(int(s[i].size()), 0);
  }
  for (int j = 0, ThxDem = int(s[0].size()); j < ThxDem; ++j) ac[0][j] = j + 1;
  for (int i = 1, ThxDem = n; i < ThxDem; ++i) {
    int r = 0;
    for (int j = 0, ThxDem = int(s[i].size()); j < ThxDem; ++j) {
      while (r < int(s[i - 1].size()) && !cmp(g[i][j], g[i - 1][r])) r++;
      if (r >= 1) ac[i][j] = ac[i - 1][r - 1];
      if (j) ac[i][j] = sum(ac[i][j], ac[i][j - 1]);
    }
  }
  cout << ac[n - 1].back() << "\n";
}