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;
constexpr int maxn = 1000000 + 10;
struct {
  int m;
  int d[maxn], ds[maxn], dt[maxn];
  int nd[maxn], nds[maxn], ndt[maxn];
  void nfy(int* d, int n, int* nd) {
    for (int i = 0; i < n; i += 1) nd[i] = n;
    for (int i = n - 1; i >= 0; i -= 1) {
      if (i + 1 < n) nd[i] = nd[i + 1];
      if (d[i]) nd[i] = i;
    }
  }
  void init(string& s, const string& t) {
    m = s.size();
    for (int i = 0; i < m; i += 1) d[i] = s[i] - t[i];
    for (int i = 0; i < m - 1; i += 1) ds[i] = s[i + 1] - t[i];
    for (int i = 0; i < m - 1; i += 1) dt[i] = s[i] - t[i + 1];
    nfy(d, m, nd);
    nfy(ds, m - 1, nds);
    nfy(dt, m - 1, ndt);
  }
  int sgn(int x) { return x < 0 ? -1 : 1; }
  int res(int i, int j) {
    int x = min(i, j);
    if (nd[0] < x) return sgn(d[nd[0]]);
    if (i == j) {
      if (i + 1 == m) return 0;
      if (nd[i + 1] == m) return 0;
      return sgn(d[nd[i + 1]]);
    }
    if (i < j) {
      if (nds[i] < j) return sgn(ds[nds[i]]);
      if (j + 1 == m) return 0;
      if (nd[j + 1] == m) return 0;
      return sgn(d[nd[j + 1]]);
    }
    if (ndt[j] < i) return sgn(dt[ndt[j]]);
    if (i + 1 == m) return 0;
    if (nd[i + 1] == m) return 0;
    return sgn(d[nd[i + 1]]);
  }
} cmp[4];
constexpr int mod = 1000000007;
void add(int& x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  string s = "#";
  vector<int> ds = {1};
  for (int i = 0; i < n; i += 1) {
    string t;
    cin >> t;
    t.push_back('#');
    vector<int> dt(t.size());
    while (s.size() < t.size()) {
      s.push_back('#');
      ds.push_back(0);
    }
    while (s.size() > t.size()) {
      t.push_back('#');
      dt.push_back(0);
    }
    int m = s.size();
    cmp[0].init(s, s);
    cmp[1].init(s, t);
    cmp[2].init(t, s);
    cmp[3].init(t, t);
    vector<pair<int, int>> vp;
    for (int i = 0; i < m; i += 1) {
      vp.push_back({i, 0});
      vp.push_back({i, 1});
    }
    sort(vp.begin(), vp.end(),
         [&](const pair<int, int>& p1, const pair<int, int>& p2) {
           int res = cmp[p1.second * 2 + p2.second].res(p1.first, p2.first);
           if (res) return res < 0;
           return p1.second < p2.second;
         });
    int sum = 0;
    for (auto [x, y] : vp) {
      if (y == 0)
        add(sum, ds[x]);
      else
        add(dt[x], sum);
    }
    swap(s, t);
    swap(ds, dt);
    while ((int)s.size() >= 2 and s[s.size() - 2] == '#') {
      s.pop_back();
      ds.pop_back();
    }
  }
  int ans = 0;
  for (auto x : ds) add(ans, x);
  cout << ans;
  return 0;
}