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 long long MOD = 1e9 + 7, p = 41;
long long mod = 2e9 + 153;
vector<long long> pows(1e6 + 1);
long long add(long long a, long long b) {
  a += b;
  if (a < 0) a += mod;
  if (a >= MOD) a -= mod;
  return a;
}
long long mul(long long a, long long b) { return (a * b) % mod; }
void get_h(string s, vector<long long>& h) {
  h[0] = s[0];
  for (long long i = 1; i < s.size(); i++) {
    h[i] = add(mul(h[i - 1], p), s[i]);
  }
}
long long get_subhash(long long l, long long r, vector<long long>& h) {
  if (r < l) {
    return 0;
  }
  long long ans = h[r];
  if (l != 0) {
    ans = add(h[r], -mul(h[l - 1], pows[r - l + 1]));
  }
  return ans;
}
long long get_hash(vector<long long>& h, long long x, long long r) {
  if (r < x) {
    return h[r - 1];
  }
  return add(mul(get_subhash(0, x - 1, h), pows[r - x]),
             get_subhash(x + 1, r, h));
}
long long get_c(string& s, long long pos, long long x) {
  if (x < pos) {
    return s[x];
  }
  return (x + 1 < s.size()) ? s[x + 1] : ' ';
}
bool smaller(string& s1, string& s2, vector<long long>& h1,
             vector<long long>& h2, long long pos1, long long pos2) {
  long long l = 0, r = min(s1.size(), s2.size()) + 1;
  while (l != r - 1) {
    long long now = (l + r) / 2;
    if (get_hash(h1, pos1, now) == get_hash(h2, pos2, now)) {
      l = now;
    } else {
      r = now;
    }
  }
  if (get_c(s1, pos1, l) <= get_c(s2, pos2, l)) {
    return 1;
  } else {
    return 0;
  }
}
void debug(string s, vector<vector<long long>>& mas) {
  cout << endl;
  cout << s << endl;
  for (long long i = 0; i < mas.size(); i++) {
    for (long long j = 0; j < mas[i].size(); j++) {
      cout << mas[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  pows[0] = 1;
  for (long long i = 1; i <= 1e6; i++) {
    pows[i] = mul(pows[i - 1], p);
  }
  vector<vector<long long>> hashes(n), dp(n), nxt(n);
  vector<vector<long long>> srt(n);
  vector<string> strs;
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    hashes[i].resize(s.size());
    dp[i].assign(s.size() + 1, i == 0 ? 1 : 0);
    get_h(s, hashes[i]);
    strs.push_back(s);
    stack<long long> st;
    nxt[i].resize(s.size(), -1);
    srt[i].resize(s.size());
    for (long long j = 0; j < s.size(); j++) {
      while (st.size() && s[j] != s[st.top()]) {
        nxt[i][st.top()] = j;
        st.pop();
      }
      st.push(j);
    }
    long long l = 0, r = s.size() - 1;
    long long pos;
    for (long long j = 0; j < s.size(); j++) {
      if (nxt[i][j] != -1 && s[nxt[i][j]] > s[j]) {
        pos = r;
        srt[i][r--] = j;
      } else {
        pos = l;
        srt[i][l++] = j;
      }
    }
    srt[i].insert(srt[i].begin() + pos + 1, s.size());
  }
  for (long long i = 1; i < n; i++) {
    long long p = 0;
    long long ans = 0;
    for (long long j = 0; j < dp[i].size(); j++) {
      while (p != srt[i - 1].size() &&
             smaller(strs[i - 1], strs[i], hashes[i - 1], hashes[i],
                     srt[i - 1][p], srt[i][j])) {
        p++;
        ans += dp[i - 1][p - 1];
        ans %= MOD;
      }
      dp[i][j] = ans;
    }
  }
  long long summ = 0;
  for (long long i = 0; i < dp[n - 1].size(); i++) {
    summ = (summ + dp[n - 1][i]) % MOD;
  }
  cout << summ << endl;
}