#include <bits/stdc++.h>   const int N = 110000;   using namespace std;   set<pair<int, int> > elements;   set<pair<int, int> >::iterator it;   int n;   int ar[N];   int next_lucky[N], prev_lucky[N];   bool is_lucky[N];   vector<int> luck;   long long ans;   long long BIN[N];   set<int> banned_set;   set<pair<int, int> > seg;   int banned[N];   long long cur_cnt;   long long bin_sum[N];   bool lucky(int x) {     if (x == 0) return 1;     if (x % 10 != 4 && x % 10 != 7) return 0;     return lucky(x / 10);   }   void add_segment(int l, int r) {     cur_cnt += BIN[r - l + 1];     cur_cnt %= 5000000000000000000ll;   }   void remove_segment(int l, int r) {     cur_cnt -= BIN[r - l + 1];     if (cur_cnt < 0) cur_cnt += 5000000000000000000ll;   }   void ban_position(int ps) {     set<int>::iterator it;     it = banned_set.lower_bound(ps);     int r = (*it);     --it;     int l = (*it);     remove_segment(l + 1, r - 1);     if (l < ps - 1) add_segment(l + 1, ps - 1);     if (r > ps + 1) add_segment(ps + 1, r - 1);     banned_set.insert(ps);   }   void show_intervals() {     set<pair<int, int> >::iterator it;     for (it = seg.begin(); it != seg.end(); ++it) {       pair<int, int> P = (*it);       cout << P.first <<     << P.second << endl;     }   }   int main() {     ios_base::sync_with_stdio(0);     for (int i = 1; i <= 100000; i++)       BIN[i] = (i + 1) * 1ll * i / 2, BIN[i] %= 5000000000000000000ll;     for (int i = 1; i <= 100000; i++) {       bin_sum[i] = bin_sum[i - 1] + BIN[i];       bin_sum[i] %= 5000000000000000000ll;     }     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> ar[i];       elements.insert(make_pair(ar[i], i));       if (lucky(ar[i])) {         is_lucky[i] = 1;         luck.push_back(i);       }     }     next_lucky[n + 1] = n + 1;     for (int i = n; i; --i) {       if (is_lucky[i])         next_lucky[i] = i;       else         next_lucky[i] = next_lucky[i + 1];     }     for (int i = 1; i <= n; i++) {       int cnt1 = next_lucky[i] - i;       ans += cnt1 * BIN[i - 1] % 5000000000000000000ll,           ans %= 5000000000000000000ll;     }     for (int i = 1; i <= n; i++) {       if (is_lucky[i])         prev_lucky[i] = i;       else         prev_lucky[i] = prev_lucky[i - 1];     }     for (int i = 0; i < luck.size(); i++) {       int id = luck[i];       seg.clear();       cur_cnt = 0;       add_segment(1, id - 1);       for (int j = 0; j < luck.size(); j++) banned[j] = 0;       banned_set.clear();       banned_set.insert(id);       banned_set.insert(0);       for (int j = i; j < luck.size(); j++) {         int id = luck[j];         int val = ar[id];         int pp = -1;         while (true) {           pair<int, int> P = make_pair(val, pp + 1);           it = elements.lower_bound(P);           if (it == elements.end()) break;           pair<int, int> P2 = (*it);           if (P2.first != P.first) break;           if (P2.second >= luck[i]) break;           if (banned_set.find(P2.second) != banned_set.end()) break;           ban_position(P2.second);           pp = P2.second;         }         int bound = n + 1;         if (j + 1 < luck.size()) bound = luck[j + 1];         long long ways = cur_cnt;         int prev = 0;         if (i) prev = luck[i - 1];         ways = ways * (luck[i] - prev) % 5000000000000000000ll;         set<int>::iterator it;         it = banned_set.lower_bound(luck[i]);         int Q1, Q2;         if (it == banned_set.begin())           Q1 = 0;         else {           --it;           Q1 = (*it);         }         Q2 = luck[i];         ways -= (BIN[Q2 - Q1 - 1]) * (luck[i] - prev) % 5000000000000000000ll;         if (ways < 0) ways += 5000000000000000000ll;         int cut;         if (i == 0)           cut = 0;         else           cut = luck[i - 1];         ways += bin_sum[Q2 - Q1 - 1] - bin_sum[cut - Q1 - 1];         ways %= 5000000000000000000ll;         if (ways < 0) ways += 5000000000000000000ll;         ways = ways * (bound - luck[j]) % 5000000000000000000ll;         ;         ans += ways;         ans %= 5000000000000000000ll;       }     }     cout << ans << endl;     cin.get();     cin.get();     return 0;   }