1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<bits/stdc++.h>
using namespace std;

#define all(X) (X).begin(), (X).end()
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;

inline ll randLong(){
    long long r = 0;
    for (int i = 0; i < 5; ++i)
        r = (r << 15) | (rand() & 0x7FFF);
    return (ll)((r & 0xFFFFFFFFFFFFFFFFULL)>>2);
}

struct mint {
    const static int32_t MODD = 1e9+7;
    int32_t value;
    mint(int32_t v=0) : value(v >= 0 ? v%MODD : v%MODD+MODD) {}
    friend mint operator+(mint a, mint b) {a.value += b.value; if(a.value >= MODD) a.value -= MODD; return a;}
    friend mint operator-(mint a, mint b) {a.value -= b.value; if(a.value < 0) a.value += MODD; return a;}
    friend mint operator*(mint a, mint b) {int64_t res = a.value; res = (res*b.value)%MODD; if(res < 0) res += MODD; return mint(res);}
   
    friend istream& operator>>(istream& in,  mint& a) { return in >> a.value; }
    friend ostream& operator<<(ostream& out, mint a) { return out << a.value; }
    
    mint& operator+=(const mint& b) {return *this = *this+b;}
};

struct Node {
    int key;
    ll pr;
    int left=-1, right=-1;
    
    mint e=0,o=0;
    mint dp;
    mint keyUpdate = 0;

    Node (int _key, ll _pr, int _dp) : key(_key), pr(_pr), dp(_dp) {}
};

int root;
vector<Node> nodes;
 
void propagate(int cur){
    if(cur == -1) return;
    if(nodes[cur].keyUpdate.value != 0) {
        if(nodes[cur].left != -1)
            nodes[nodes[cur].left].keyUpdate += nodes[cur].keyUpdate;
        if(nodes[cur].right != -1)
            nodes[nodes[cur].right].keyUpdate += nodes[cur].keyUpdate;

        int v1 = nodes[cur].key;
        nodes[cur].key = (nodes[cur].keyUpdate + nodes[cur].key).value;
        nodes[cur].keyUpdate = 0;
        if(v1%2 != nodes[cur].key%2)
            swap(nodes[cur].e, nodes[cur].o);
        
    }
}

int attach(int cur, int left, int right){
    nodes[cur].left = left;
    nodes[cur].right = right;
    nodes[cur].e = nodes[cur].o = 0;
    if(nodes[cur].key%2 == 0)
        nodes[cur].e = nodes[cur].dp;
    else
        nodes[cur].o = nodes[cur].dp;

    if(left != -1) {
        propagate(left);
        nodes[cur].e += nodes[left].e;
        nodes[cur].o += nodes[left].o;
    }
    if(right != -1) {
        propagate(right);
        nodes[cur].e += nodes[right].e;
        nodes[cur].o += nodes[right].o;
    }
    return cur;
}



int merge(int left,int right){
    if(left == -1)
        return right;
    if(right == -1)
        return left;

    propagate(left);
    propagate(right);

    if(nodes[left].pr < nodes[right].pr) {
        int tmp = merge(nodes[left].right, right);
        return attach(left,nodes[left].left,tmp);
    }
    else{
        int tmp = merge(left,nodes[right].left);
        return attach(right,tmp,nodes[right].right);
    }
}

inline pii split(int cur, int val){
    if(cur == -1)
        return {-1,-1};

    propagate(cur);
    if(nodes[cur].key > val){
        auto tmp = split(nodes[cur].left, val);
        cur = attach(cur, tmp.nd, nodes[cur].right);
        return {tmp.st,cur};
    }
    else{
        auto tmp = split(nodes[cur].right, val);
        cur = attach(cur, nodes[cur].left, tmp.st);
        return {cur,tmp.nd};
    }
}

inline void insert(int dpval) {
    nodes.push_back(Node(0, randLong(), dpval));
    attach(nodes.size()-1,-1,-1);
    if(nodes.size() == 1) return;
    root = merge(nodes.size()-1, root);
}

const int MOD = 1e9+7;
void shiftKeys(int x) {
    auto s = split(root, MOD-x-1);
    if(s.nd != -1)
        nodes[s.nd].keyUpdate += x-MOD;
    if(s.st != -1)
        nodes[s.st].keyUpdate += x;
    root = merge(s.nd, s.st);
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,x;
    cin >> n;
    insert(1);
    int res = 0;
    for(int i=0;i<n;i++) {
        cin >> x;
        shiftKeys(x);
        propagate(root);
        res = nodes[root].e.value;
        insert(res);
    }
    cout<<res;
}