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;
mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
int uniform(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}
template <int MOD>
struct str_hash {
  static int P;
  int n;
  string s;
  vector<long long> h, power;
  str_hash() {}
  str_hash(string s_) : n(s_.size()), s(s_), h(n), power(n) {
    power[0] = 1;
    for (int i = 1; i < n; i++) power[i] = power[i - 1] * P % MOD;
    h[0] = s[0];
    for (int i = 1; i < n; i++) h[i] = (h[i - 1] * P + s[i]) % MOD;
  }
  long long hash(int i, int j) {
    if (j < i) return 0;
    if (!i) return h[j];
    long long ret = h[j] - h[i - 1] * power[j - i + 1] % MOD;
    return ret < 0 ? ret + MOD : ret;
  }
  long long hash_remove(int p, int x) {
    if (x > p) return hash(0, p);
    long long ret = (hash(0, p) - hash(0, x) * power[p - x] +
                     hash(0, x - 1) * power[p - x]) %
                    MOD;
    return ret < 0 ? ret + MOD : ret;
  }
};
template <int MOD>
int str_hash<MOD>::P = uniform(29, MOD - 1);
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];
str_hash<mod> hashes[MAX];
bool comp(int i, int j, int vi, int vj) {
  int l = 0;
  int r = min(s[i].size(), s[j].size()) - 2;
  int at_i = 0;
  int at_j = 0;
  while (true) {
    int mid = (l + r) / 2;
    at_i = mid + (mid >= vi ? 1 : 0);
    at_j = mid + (mid >= vj ? 1 : 0);
    if (l == r) break;
    if (hashes[i].hash_remove(at_i, vi) == hashes[j].hash_remove(at_j, vj))
      l = mid + 1;
    else
      r = mid;
  }
  auto cmp = [&](char a, char b) -> bool {
    if (a == '#' and b == '!') return 0;
    return a > b;
  };
  return cmp(s[i][at_i], s[j][at_j]);
}
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, x + 1, 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());
    hashes[i] = str_hash<mod>(s[i]);
  }
  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);
}