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;
int M[2];
struct str {
  int r[2];
  str(int x = 0) {
    for (int i = 0; i < 2; ++i) r[i] = x;
  }
  str& operator+=(str b) {
    for (int i = 0; i < 2; ++i) r[i] = (r[i] + b.r[i]) % M[i];
    return *this;
  }
  friend str operator+(str a, str b) { return a += b; }
  str& operator-=(str b) {
    for (int i = 0; i < 2; ++i) r[i] = (r[i] - b.r[i] + M[i]) % M[i];
    return *this;
  }
  friend str operator-(str a, str b) { return a -= b; }
  str& operator*=(str b) {
    for (int i = 0; i < 2; ++i) r[i] = (1LL * r[i] * b.r[i]) % M[i];
    return *this;
  }
  friend str operator*(str a, str b) { return a *= b; }
};
bool operator==(str a, str b) {
  for (int i = 0; i < 2; ++i)
    if (a.r[i] != b.r[i]) return false;
  return true;
}
str st[1000001];
vector<str> pr[100000];
int word;
string a[100000];
str get_str(int word1, int x, int len) {
  if (len <= x) return pr[word1][len];
  return pr[word1][x] * st[len - x] + pr[word1][len + 1] -
         pr[word1][x + 1] * st[len - x];
}
vector<int> b[100000], dp[100000];
bool cmp2(int x, int y) {
  int k1 = a[word].size(), k2 = a[word + 1].size();
  if (x != k1) k1--;
  if (y != k2) k2--;
  int l = 0, r = min(k1, k2) + 1;
  while (l < r - 1) {
    int m = (l + r) / 2;
    if (get_str(word, x, m) == get_str(word + 1, y, m))
      l = m;
    else
      r = m;
  }
  if (l == min(k1, k2)) return k1 <= k2;
  int l1 = l, l2 = l;
  if (l1 >= x) l1++;
  if (l2 >= y) l2++;
  return a[word][l1] <= a[word + 1][l2];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  M[0] = 1000000007;
  M[1] = 1000000009;
  int base = 200;
  st[0] = 1;
  for (int i = 0; i < 1000000; ++i) st[i + 1] = st[i] * base;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) {
    pr[i].resize(a[i].size() + 1);
    for (int j = 0; j < a[i].size(); ++j)
      pr[i][j + 1] = pr[i][j] * base + a[i][j];
  }
  for (int i = 0; i < n; ++i) {
    vector<int> nxt(a[i].size(), a[i].size() * 1LL - 1);
    for (int j = a[i].size() * 1LL - 2; j >= 0; --j) {
      if (a[i][j] == a[i][j + 1])
        nxt[j] = nxt[j + 1];
      else
        nxt[j] = j + 1;
    }
    int l = 0, r = a[i].size();
    vector<int> a1(a[i].size());
    r--;
    for (int j = 0; j < a[i].size(); ++j) {
      if (a[i][j] >= a[i][nxt[j]])
        a1[l] = j, l++;
      else
        a1[r] = j, r--;
    }
    for (int j = 0; j < a[i].size(); ++j) {
      b[i].push_back(a1[j]);
      if (b[i].back() == a[i].size() * 1LL - 1) b[i].push_back(a[i].size());
    }
  }
  int mod = 1000000007;
  dp[n - 1].resize(a[n - 1].size() + 1);
  for (int i = 0; i < a[n - 1].size() + 1; ++i) dp[n - 1][i] = 1;
  for (int i = n - 2; i >= 0; --i) {
    dp[i].resize(a[i].size() + 1);
    word = i;
    int s = 0;
    int j1 = a[i + 1].size() + 1;
    for (int j = a[i].size(); j >= 0; --j) {
      while (j1 > 0 && cmp2(b[i][j], b[i + 1][j1 - 1]))
        j1--, s += dp[i + 1][j1], s %= mod;
      dp[i][j] = s;
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) ans += dp[0][i], ans %= mod;
  cout << ans;
  return 0;
}