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;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
int n;
string s[MAX];
vector<int> dp[MAX];
vector<int> ord[MAX];
vector<int> lcp[MAX], lcp_l[MAX], lcp_r[MAX];
vector<int> lcp_self[MAX];
void pre_lcp() {
  for (int i = 0; i < n - 1; i++) {
    {
      int ans = 0;
      for (int j = 0; j < s[i].size(); j++) {
        ans = max(0, ans - 1);
        while (j + ans < s[i + 1].size() and s[i + 1][j + ans] == s[i][j + ans])
          ans++;
        lcp[i][j] = ans;
      }
    }
    {
      int ans = 0;
      for (int j = 0; j < s[i].size(); j++) {
        ans = max(0, ans - 1);
        while (j + 1 + ans < s[i + 1].size() and
               s[i + 1][j + 1 + ans] == s[i][j + ans])
          ans++;
        lcp_l[i][j] = ans;
      }
    }
    {
      int ans = 0;
      for (int j = 0; j < s[i].size(); j++) {
        ans = max(0, ans - 1);
        while (j - 1 + ans >= 0 and j - 1 + ans < s[i + 1].size() and
               s[i + 1][j - 1 + ans] == s[i][j + ans])
          ans++;
        lcp_r[i][j] = ans;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int ans = 0;
    for (int j = 0; j < s[i].size(); j++) {
      ans = max(0, ans - 1);
      while (j + ans < s[i].size() and s[i][j + ans] == s[i][j]) ans++;
      lcp_self[i][j] = ans;
    }
  }
}
bool comp(int i, int vi, int vj) {
  auto cmp = [&](char a, char b) -> bool {
    if (a == '#' and b == '!') return 1;
    return a < b;
  };
  if (lcp[i][0] < min(vi, vj)) return cmp(s[i][lcp[i][0]], s[i + 1][lcp[i][0]]);
  if (vi == vj) {
    int at = vi + 1 + lcp[i][vi + 1];
    if (at == s[i].size()) return 1;
    return cmp(s[i][at], s[i + 1][at]);
  }
  if (vi < vj) {
    int at = vi + 1 + lcp_r[i][vi + 1];
    if (at <= vj) return s[i][at] < s[i + 1][at - 1];
    int at2 = vj + 1 + lcp[i][vj + 1];
    if (at2 == s[i].size()) return 1;
    return cmp(s[i][at2], s[i + 1][at2]);
  } else {
    int at = vj + lcp_l[i][vj];
    if (at < vi) return s[i][at] < s[i + 1][at + 1];
    int at2 = vi + 1 + lcp[i][vi + 1];
    if (at2 == s[i].size()) return 1;
    return cmp(s[i][at2], s[i + 1][at2]);
  }
}
int comp_self(int i, int vi, int vj) {
  if (vi == vj) return 0;
  if (vi > vj) {
    int at = vj + lcp_self[i][vj] - 1;
    if (at < vi) return s[i][at] < s[i][at + 1];
  } else {
    int at = vi + lcp_self[i][vi] - 1;
    if (at < vj) return s[i][at] > s[i][at + 1];
  }
  return 0;
}
void solve(int x) {
  if (x == n - 1) {
    for (int i = 0; i < s[x].size() - 1; i++) dp[x].push_back(1);
    return;
  }
  solve(x + 1);
  int sum = 0;
  int j = 0;
  for (int i = 0; i < s[x].size() - 1; i++) {
    while (j < s[x + 1].size() - 1 && comp(x, ord[x][i], ord[x + 1][j])) {
      sum = (sum + dp[x + 1][j]) % MOD;
      j++;
    }
    dp[x].push_back(sum);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    s[i] += '#';
    s[i] += '!';
    lcp[i].resize(s[i].size());
    lcp_l[i].resize(s[i].size());
    lcp_r[i].resize(s[i].size());
    lcp_self[i].resize(s[i].size());
  }
  pre_lcp();
  for (int i = 0; i < n; i++) {
    vector<int> at(s[i].size() - 1);
    iota(at.begin(), at.end(), 0);
    sort(at.begin(), at.end(),
         [&](int a, int b) { return comp_self(i, a, b); });
    reverse(at.begin(), at.end());
    for (int j : at) ord[i].push_back(j);
  }
  solve(0);
  int ret = 0;
  for (auto x : dp[0]) ret = (ret + x) % MOD;
  cout << ret << '\n';
  exit(0);
}