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 N = 2e6 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const long long LNF = 0x3f3f3f3f3f3f3f3f;
int has[N], w[N];
int bas = 233333;
void init() {
  w[0] = 1;
  for (int i = 1; i < N; ++i) {
    w[i] = 1LL * w[i - 1] * bas % mod;
  }
}
void build(int *s, int len) {
  has[0] = 0;
  for (int i = 1; i <= len; ++i) {
    has[i] = (1LL * has[i - 1] * bas + (s[i - 1] - 'a' + 1)) % mod;
  }
}
inline int get(int l, int r) {
  ++l;
  ++r;
  return (has[r] - 1LL * has[l - 1] * w[r - l + 1] % mod + mod) % mod;
}
int s[N];
string str[N];
int st[N], nx[N];
vector<int> sorted[N];
int Dp[2][N];
inline int lcp(int x, int y, int up) {
  int l = 0, r = up + 1, mid;
  while (r > l) {
    mid = r + l >> 1;
    if (get(x, x + mid - 1) == get(y, y + mid - 1)) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return r - 1;
}
inline void ssort(int id) {
  string &ts = str[id];
  sorted[id].resize(str[id].length() + 1);
  int l = 0, r = str[id].length();
  nx[ts.length() - 1] = ts.length() - 1;
  for (int i = ts.length() - 2; i >= 0; --i) {
    nx[i] = ts[i] == ts[i + 1] ? nx[i + 1] : i + 1;
  }
  for (int i = 0; i < ts.length(); ++i) {
    if (ts[i] < ts[nx[i]]) {
      sorted[id][r--] = i;
    } else {
      sorted[id][l++] = i;
    }
  }
  sorted[id][r--] = ts.length();
}
inline int cmp(int x, int xp, int y, int yp) {
  int sx = 0, sy = 0;
  while (1) {
    if (sx == xp && sx < str[x].length()) {
      sx++;
    }
    if (sy == yp && sy < str[y].length()) {
      sy++;
    }
    int lx = sx < xp ? xp - sx : str[x].length() - sx;
    int ly = sy < yp ? yp - sy : str[y].length() - sy;
    if (!lx || !ly) {
      return lx <= ly;
    }
    int l = min(lx, ly);
    int mt = lcp(st[x] + sx, st[y] + sy, l);
    if (mt < l) {
      return str[x][sx + mt] < str[y][sy + mt];
    }
    sx += l;
    sy += l;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  init();
  int n;
  cin >> n;
  int len = 0;
  for (int i = 0; i < n; ++i) {
    cin >> str[i];
    st[i] = len;
    for (int j = 0; j < str[i].length(); ++j) {
      s[len++] = str[i][j];
    }
    s[len++] = 130 + i;
  }
  build(s, len);
  for (int i = 0; i < n; ++i) {
    ssort(i);
  }
  int cur = 0;
  for (int i = 0; i <= str[0].length(); ++i) {
    Dp[cur][i] = 1;
  }
  for (int i = 1; i < n; ++i) {
    cur ^= 1;
    int p = 0, sum = 0;
    for (int j = 0; j < sorted[i].size(); ++j) {
      while (p < sorted[i - 1].size() &&
             cmp(i - 1, sorted[i - 1][p], i, sorted[i][j])) {
        sum += Dp[cur ^ 1][sorted[i - 1][p++]];
        if (sum >= mod) {
          sum -= mod;
        }
      }
      Dp[cur][sorted[i][j]] = sum;
    }
  }
  int ans = 0;
  for (int i = 0; i <= str[n - 1].length(); ++i) {
    ans += Dp[cur][i];
    if (ans >= mod) {
      ans -= mod;
    }
  }
  cout << ans << endl;
}