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;
template <class T>
ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}
template <class T>
istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}
template <class T>
ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}
template <class T>
istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}
void solve();
signed main() {
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}
const long long MOD = 1e9 + 7, MAXN = 1e6 + 10, INF = 1e18 + 10, BASE = 31;
long long n;
vector<vector<long long>> h;
vector<string> s;
vector<long long> p(MAXN);
char get_ch(long long i, long long id, long long cnt) {
  if (cnt < id) {
    return s[i][cnt];
  } else {
    cnt++;
    if (cnt >= (long long)((s[i]).size())) {
      return (char)('a' - 1 - ('a' - 2));
    } else {
      return s[i][cnt];
    }
  }
}
long long get_hash(long long i, long long id, long long len) {
  if (len < id + 1) {
    return h[i][len - 1];
  } else {
    if (id == 0) {
      return (h[i][len] - (long long)s[i][0] * p[len] % MOD + MOD * MOD) % MOD;
    } else {
      long long h1 = h[i][id - 1];
      long long h2 = (h[i][len] - s[i][id] * p[len - id] % MOD + MOD * MOD -
                      ((BASE - 1) * h1 % MOD) * p[len - id] % MOD) %
                     MOD;
      return h2;
    }
  }
}
bool smaller_or_equal(long long i1, long long id1, long long i2,
                      long long id2) {
  long long len = min((long long)((h[i1]).size()), (long long)((h[i2]).size()));
  long long l = -1, r = len;
  while (r - l > 1) {
    long long mid = (r + l) / 2;
    long long h1 = get_hash(i1, id1, mid + 1), h2 = get_hash(i2, id2, mid + 1);
    if (h1 == h2) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (l == -1) {
    return get_ch(i1, id1, 0) <= get_ch(i2, id2, 0);
  }
  if (l == len - 1) {
    return (long long)((h[i1]).size()) <= (long long)((h[i2]).size());
  } else {
    return get_ch(i1, id1, l + 1) <= get_ch(i2, id2, l + 1);
  }
}
void solve() {
  p[0] = 1;
  for (long long i = 1; i < MAXN; ++i) {
    p[i] = p[i - 1] * BASE % MOD;
  }
  cin >> n;
  h.resize(n + 1);
  s.resize(n + 1);
  s[0].push_back('a' - 1);
  vector<vector<long long>> dp(n + 1);
  for (long long i = 1; i <= n; ++i) {
    cin >> s[i];
    s[i].push_back('a' - 1);
    for (long long j = 0; j < (long long)((s[i]).size()); ++j) {
      s[i][j] -= ('a' - 2);
    }
    dp[i].resize((long long)((s[i]).size()));
    h[i].resize((long long)((s[i]).size()));
    h[i][0] = s[i][0];
    for (long long j = 1; j < (long long)((h[i]).size()); ++j) {
      h[i][j] = (h[i][j - 1] * BASE + s[i][j]) % MOD;
    }
  }
  dp[0].push_back(1);
  h[0].push_back(s[0][0]);
  vector<vector<long long>> id(n + 1);
  for (long long i = 1; i <= n; ++i) {
    vector<long long> nxt((long long)((s[i]).size()));
    long long ind = (long long)((s[i]).size()) - 1;
    long long j = (long long)((s[i]).size()) - 2;
    while (j >= 0) {
      char tmp = s[i][j];
      while (j >= 0 && s[i][j] == tmp) {
        nxt[j] = ind;
        j--;
      }
      ind = j + 1;
    }
    vector<long long> res((long long)((s[i]).size()) - 1);
    long long L = 0, R = (long long)((res).size()) - 1;
    for (long long j = 0; j < (long long)((s[i]).size()) - 1; ++j) {
      if (s[i][j] < s[i][nxt[j]]) {
        res[R--] = j;
      } else {
        res[L++] = j;
      }
    }
    if (res.back() == (long long)((res).size()) - 1) {
      res.push_back((long long)((res).size()));
    } else {
      for (long long j = 0; j < (long long)((res).size()); ++j) {
        if (res[j] == (long long)((res).size()) - 1) {
          res.insert(res.begin() + j + 1, (long long)((res).size()));
          break;
        }
      }
    }
    id[i] = res;
  }
  id[0].push_back(0);
  for (long long i = 1; i <= n; ++i) {
    long long sum = 0;
    long long j1 = 0;
    for (long long j2 = 0; j2 < (long long)((id[i]).size()); ++j2) {
      while (j1 < (long long)((id[i - 1]).size()) &&
             smaller_or_equal(i - 1, id[i - 1][j1], i, id[i][j2])) {
        sum = (dp[i - 1][id[i - 1][j1]] + sum) % MOD;
        j1++;
      }
      dp[i][id[i][j2]] = sum;
    }
  }
  long long ans = 0;
  for (long long j = 0; j < (long long)((dp[n]).size()); ++j) {
    ans = (ans + dp[n][j]) % MOD;
  }
  cout << ans << '\n';
}