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;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
template <class T = int>
T __rand(T range = numeric_limits<T>::max()) {
  return (T)(rng() % range);
}
template <class T>
ostream& operator,(ostream& out, const T& thing) {
  return out << ", " << thing;
}
template <class U, class V>
ostream& operator<<(ostream& out, const pair<U, V>& p) {
  return (out << "(" << p.first, p.second) << ")";
}
template <class A, class B>
ostream& operator<<(ostream& out, const tuple<A, B>& t) {
  return (out << "(" << get<0>(t), get<1>(t)) << ")";
}
template <class A, class B, class C>
ostream& operator<<(ostream& out, const tuple<A, B, C>& t) {
  return (out << "(" << get<0>(t), get<1>(t), get<2>(t)) << ")";
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& container) {
  out << "{";
  if (((int)container.size())) out << container[0];
  for (int i = 0; i++ < ((int)container.size()) - 1;) out, container[i];
  return out << "}";
}
template <class x>
vector<typename x::value_type> $v(const x& a) {
  return vector<typename x::value_type>(a.begin(), a.end());
}
template <class u>
vector<typename iterator_traits<u>::value_type> $v(u a, u b) {
  return vector<typename iterator_traits<u>::value_type>(a, b);
}
function<bool(int, int)> gen_missing_char_cmp(const string& su,
                                              const string& sv) {
  using sitor = string::const_iterator;
  static auto gen_diff = [](sitor bu, sitor eu, sitor bv, sitor ev) {
    int lu = (int)(eu - bu), lv = (int)(ev - bv);
    int l = min(lu, lv);
    vector<int> ans(l + 1);
    ans.back() = l;
    int prev = l;
    for (int i = l; i--;) {
      if (bu[i] == bv[i])
        ans[i] = prev;
      else
        prev = ans[i] = i;
    }
    return ans;
  };
  static vector<int> first_diff, miss1_first_diff, miss2_first_diff;
  first_diff = gen_diff(su.begin(), su.end(), sv.begin(), sv.end());
  miss1_first_diff = gen_diff(su.begin() + 1, su.end(), sv.begin(), sv.end());
  miss2_first_diff = gen_diff(su.begin(), su.end(), sv.begin() + 1, sv.end());
  return [&su, &sv](int u, int v) {
    auto cmp_char = [&](int x, int y) {
      char cu = x >= ((int)su.size()) ? ' ' : su[x];
      char cv = y >= ((int)sv.size()) ? ' ' : sv[y];
      return cu < cv;
    };
    if (int t = first_diff[0]; t < min(u, v)) return cmp_char(t, t);
    if (u < v) {
      if (int t = miss1_first_diff[u]; t < v) return cmp_char(t + 1, t);
    } else {
      if (int t = miss2_first_diff[v]; t < u) return cmp_char(t, t + 1);
    }
    int t = first_diff[max(u, v) + 1];
    return cmp_char(t, t);
  };
}
const long long mod = (long long)1e9 + 7;
const int maxn = 101010;
int n;
string s[maxn];
vector<int> mchs[maxn];
vector<long long> dp[maxn];
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = -1; ++i < n;) {
    ;
    cin >> s[i];
    s[i] += ' ';
    ;
    for (int f = -1; ++f < ((int)s[i].size());) mchs[i].push_back(f);
    auto cur_cmp = gen_missing_char_cmp(s[i], s[i]);
    sort(mchs[i].begin(), mchs[i].end(), cur_cmp);
    if (i == 0) {
      dp[i].resize(((int)s[i].size()), 1);
      continue;
    }
    dp[i].resize(((int)s[i].size()));
    long long sum_prev = 0;
    int u = 0;
    auto cmp = gen_missing_char_cmp(s[i], s[i - 1]);
    for (int v = -1; ++v < ((int)s[i].size());) {
      while (u < ((int)s[i - 1].size()) and
             !(cmp(mchs[i][v], mchs[i - 1][u]))) {
        sum_prev += dp[i - 1][u];
        if (sum_prev >= mod) sum_prev -= mod;
        ++u;
      }
      dp[i][v] = sum_prev;
    };
  }
  long long ans = 0;
  for (auto& i : dp[n - 1]) {
    ans += i;
    if (ans >= mod) ans -= mod;
  }
  cout << ans;
  return 0;
}