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>
using v2d = vector<vector<T>>;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 10;
const int maxS = 1e6 + 10;
const long long mod = 1e9 + 7;
const pair<long long, long long> modHash = {1000000007, 1000000009};
const long long base = 31;
int n;
string s[maxN];
vector<pair<long long, long long>> h[maxN], diff[maxN];
pair<long long, long long> pw[maxS];
vector<int> d[maxN], lcp[maxN];
vector<long long> dp[maxN];
void init() {
  pw[0] = {1, 1};
  for (int i = 1; i <= (int)(1e6); ++i) {
    pw[i].first = pw[i - 1].first * base % modHash.first;
    pw[i].second = pw[i - 1].second * base % modHash.second;
  }
}
pair<long long, long long> calPrefixHash(int i, int j, int k) {
  if (k < j) {
    return h[i][k];
  }
  pair<long long, long long> ans;
  ans.first = (h[i][k + 1].first + diff[i][j].first * pw[k + 1 - j].first) %
              modHash.first;
  ans.second = (h[i][k + 1].second + diff[i][j].second * pw[k + 1 - j].second) %
               modHash.second;
  return ans;
}
pair<long long, long long> calHash(int i, int l, int r) {
  pair<long long, long long> ans;
  if (l >= 1) {
    ans.first = (h[i][r].first - h[i][l - 1].first * pw[r - l + 1].first +
                 modHash.first * modHash.first) %
                modHash.first;
    ans.second = (h[i][r].second - h[i][l - 1].second * pw[r - l + 1].second +
                  modHash.second * modHash.second) %
                 modHash.second;
  } else {
    ans = h[i][r];
  }
  return ans;
}
bool compString(int i1, int j1, int i2, int j2) {
  int len1 = (int)s[i1].size() - (j1 != (int)s[i1].size()),
      len2 = (int)s[i2].size() - (j2 != (int)s[i2].size());
  int l = 0, r = min(len1, len2) + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (calPrefixHash(i1, j1, mid - 1) == calPrefixHash(i2, j2, mid - 1)) {
      l = mid;
    } else {
      r = mid;
    }
  }
  if (l == len1 || l == len2) {
    return (len1 < len2);
  }
  return (s[i1][l + (l >= j1)] < s[i2][l + (l >= j2)]);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= (int)(n); ++i) {
    cin >> s[i];
  }
  for (int i = 1; i <= (int)(n); ++i) {
    h[i].resize(s[i].size());
    diff[i].resize(s[i].size());
    pair<long long, long long> pv = {0, 0};
    for (int j = 0; j < (int)(s[i].size()); ++j) {
      h[i][j].first =
          (pv.first * base % modHash.first + s[i][j] - 'a' + 1) % modHash.first;
      h[i][j].second = (pv.second * base % modHash.second + s[i][j] - 'a' + 1) %
                       modHash.second;
      diff[i][j].first =
          (pv.first - h[i][j].first + modHash.first) % modHash.first;
      diff[i][j].second =
          (pv.second - h[i][j].second + modHash.second) % modHash.second;
      pv = h[i][j];
    }
  }
  for (int i = 1; i <= (int)(n); ++i) {
    lcp[i].resize((int)s[i].size());
    for (int j = 0; j < (int)((int)s[i].size() - 1); ++j) {
      int l = 0, r = (int)s[i].size() - j;
      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (calHash(i, j, j + mid - 1) == calHash(i, j + 1, j + mid)) {
          l = mid;
        } else {
          r = mid;
        }
      }
      lcp[i][j] = l;
    }
    lcp[i][(int)s[i].size() - 1] = 0;
  }
  for (int i = 1; i <= (int)(n); ++i) {
    for (int j = (int)(0); j <= (int)(s[i].size()); ++j) {
      d[i].emplace_back(j);
    }
    sort((d[i]).begin(), (d[i]).end(), [&](int x, int y) -> bool {
      if (x == y) {
        return 0;
      }
      if (x < y) {
        if (x == (int)s[i].size() - 1) {
          return 1;
        }
        int len = min(lcp[i][x], y - x);
        if (len == y - x) {
          return 0;
        }
        return s[i][x + len + 1] < s[i][x + len];
      }
      if (y == (int)s[i].size() - 1) {
        return 0;
      }
      int len = min(lcp[i][y], x - y);
      if (len == x - y) {
        return 0;
      }
      return s[i][y + len] < s[i][y + len + 1];
    });
  }
  for (int i = 1; i <= (int)(n); ++i) {
    dp[i].resize(d[i].size());
  }
  for (int j = 0; j < (int)(d[1].size()); ++j) {
    dp[1][j] = j + 1;
  }
  for (int i = (int)(2); i <= (int)(n); ++i) {
    int idx = -1;
    for (int j = 0; j < (int)(d[i].size()); ++j) {
      while (idx + 1 < (int)d[i - 1].size() &&
             !compString(i, d[i][j], i - 1, d[i - 1][idx + 1])) {
        idx++;
      }
      if (idx >= 0) {
        dp[i][j] = dp[i - 1][idx];
      }
      if (j >= 1) {
        dp[i][j] += dp[i][j - 1];
        if (dp[i][j] >= mod) {
          dp[i][j] -= mod;
        }
      }
    }
  }
  cout << dp[n].back();
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}