#include <bits/stdc++.h>
using namespace std;

template<typename Monoid,typename OperatorMonoid>
struct LazySegmentTree{
    typedef function<Monoid(Monoid,Monoid)> F;
    typedef function<Monoid(Monoid,OperatorMonoid)> G;
    typedef function<OperatorMonoid(OperatorMonoid,OperatorMonoid)> H;
    int n,hi;
    F f;
    G g;
    H h;
    Monoid id0;
    OperatorMonoid id1;
    vector<Monoid> dat;
    vector<OperatorMonoid> laz;
    LazySegmentTree(int n_,F f,G g,H h,Monoid id0,OperatorMonoid id1):
                    f(f),g(g),h(h),id0(id0),id1(id1){init(n_);}
    void init(int n_){
        n=1,hi=0;
        while(n<n_) n<<=1,++hi;
        dat.assign(n<<1,id0);
        laz.assign(n<<1,id1);
    }
    void build(const vector<Monoid> &v){
        for (int i=0;i<v.size();++i) dat[i+n]=v[i];
        for (int i=n-1;i;--i) dat[i]=f(dat[i<<1|0],dat[i<<1|1]);
    }
    inline Monoid reflect(int k){
        return laz[k]==id1?dat[k]:g(dat[k],laz[k]);
    }
    inline void propagate(int k){
        if (laz[k]==id1) return;
        laz[k<<1|0]=h(laz[k<<1|0],laz[k]);
        laz[k<<1|1]=h(laz[k<<1|1],laz[k]);
        dat[k]=reflect(k);
        laz[k]=id1;
    }
    inline void thrust(int k){
        for (int i=hi;i;--i) propagate(k>>i);
    }
    inline void recalc(int k){
        while(k>>=1) dat[k]=f(reflect(k<<1|0),reflect(k<<1|1));
    }
    void update(int a,int b,OperatorMonoid x){
        if (a>=b) return;
        thrust(a+=n); thrust(b+=n-1);
        for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if (l&1) laz[l]=h(laz[l],x),++l;
            if (r&1) --r,laz[r]=h(laz[r],x);
        }
        recalc(a); recalc(b);
    }
    void set_val(int k,Monoid x){
        thrust(k+=n);
        dat[k]=x,laz[k]=id1;
        recalc(k);
    }
    Monoid query(int a,int b){
        if (a>=b) return id0;
        thrust(a+=n); thrust(b+=n-1);
        Monoid vl=id0,vr=id0;
        for (int l=a,r=b+1;l<r;l>>=1,r>>=1){
            if (l&1) vl=f(vl,reflect(l++));
            if (r&1) vr=f(reflect(--r),vr);
        }
        return f(vl,vr);
    }
    template<typename C>
    int find_subtree(int k,const C &check,Monoid &M,bool type){
        while(k<n){
            propagate(k);
            Monoid nxt=type?f(reflect(k<<1|type),M):f(M,reflect(k<<1|type));
            if (check(nxt)) k=k<<1|type;
            else M=nxt,k=k<<1|(type^1);
        }
        return k-n;
    }
    // min i s.t. f(seg[a],seg[a+1],...,seg[i]) satisfy "check"
    template<typename C>
    int find_first(int a,const C &check){
        Monoid L=id0;
        if (a<=0){
            if (check(f(L,reflect(1)))) return find_subtree(1,check,L,false);
            return -1;
        }
        thrust(a+n);
        int b=n;
        for (int l=a+n,r=b+n;l<r;l>>=1,r>>=1){
            if (l&1){
                Monoid nxt=f(L,reflect(l));
                if (check(nxt)) return ffind_subtree(l,check,L,false);
                L=nxt; ++l;
            }
        }
        return -1;
    }
    // max i s.t. f(seg[i],...,seg[b-2],seg[b-1]) satisfy "check"
    template<typename C>
    int find_last(int b,const C &check){
        Monoid R=id0;
        if (b>=n){
            if (check(f(reflect(1),R))) return find_subtree(1,check,R,true);
            return -1;
        }
        thrust(b+n-1);
        int a=n;
        for (int l=a,r=b+n;l<r;l>>=1,r>>=1){
            if (r&1){
                Monoid nxt=f(reflect(--r),R);
                if (check(nxt)) return find_subtree(r,check,R,true);
                R=nxt;
            }
        }
        return -1;
    }
    Monoid operator[](int i){return query(i,i+1);}
};

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/all/DSL_2_D

void DSL_2_D(){
    int n,q; cin >> n >> q;

    auto f=[](int a,int b){return min(a,b);};
    auto g=[](int a,int b){return (~b?b:a);};
    auto h=[](int a,int b){return b;};
    LazySegmentTree<int,int> seg(n,f,g,h,INT_MAX,-1);

    for (;q--;){
        int c,s,t,x,i; cin >> c;
        if (!c){
            cin >> s >> t >> x;
            seg.update(s,t+1,x);
        } else {
            cin >> i;
            cout << seg[i] << '\n';
        }
    }
}

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_E

void DSL_2_E(){
    int n,q; cin >> n >> q;

    auto f=[](int a,int b){return a+b;};
    LazySegmentTree<int,int> seg(n,f,f,f,0,0);

    for (;q--;){
        int c,s,t,x,i; cin >> c;
        if (!c){
            cin >> s >> t >> x;
            seg.update(s-1,t,x);
        } else {
            cin >> i;
            cout << seg[i-1] << '\n';
        }
    }
}

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_F

void DSL_2_F(){
    int n,q; cin >> n >> q;

    auto f=[](int a,int b){return min(a,b);};
    auto g=[](int a,int b){return (~b?b:a);};
    auto h=[](int a,int b){return b;};
    LazySegmentTree<int,int> seg(n,f,g,h,INT_MAX,-1);

    for (;q--;){
        int c,s,t,x; cin >> c >> s >> t;
        if (!c){
            cin >> x;
            seg.update(s,t+1,x);
        } else cout << seg.query(s,t+1) << '\n';
    }
}

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_G

void DSL_2_G(){
    int n,q; cin >> n >> q;

    struct node{
        long long a; int b;
        node (long long a,int b):a(a),b(b){}
    };
    auto f=[](node a,node b){return node(a.a+b.a,a.b+b.b);};
    auto g=[](node a,long long x){return node(a.a+x*a.b,a.b);};
    auto h=[](long long a,long long b){return a+b;};
    LazySegmentTree<node,long long> seg(n,f,g,h,node(0,0),0);
    vector<node> v(n,node(0,1));
    seg.build(v);

    for (;q--;){
        int c,s,t,x; cin >> c >> s >> t;
        if (!c){
            cin >> x;
            seg.update(s-1,t,x);
        } else cout << seg.query(s-1,t).a << '\n';
    }
}

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_H

void DSL_2_H(){
    int n,q; cin >> n >> q;

    auto f=[](int a,int b){return min(a,b);};
    auto g=[](int a,int b){return a+b;};
    LazySegmentTree<int,int> seg(n,f,g,g,INT_MAX,0);
    vector<int> v(n,0);
    seg.build(v);

    for (;q--;){
        int c,s,t,x; cin >> c >> s >> t;
        if (!c){
            cin >> x;
            seg.update(s,t+1,x);
        } else cout << seg.query(s,t+1) << '\n';
    }
}

// https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/2/DSL_2_I

void DSL_2_I(){
    const int inf=1<<30;
    int n,q; cin >> n >> q;

    struct node{
        long long a; int b;
        node(long long a,int b):a(a),b(b){}
    };
    auto f=[](node a,node b){return node(a.a+b.a,a.b+b.b);};
    auto g=[](node a,long long x){return node(x!=inf?x*a.b:a.a,a.b);};
    auto h=[](long long a,long long b){return b;};
    LazySegmentTree<node,long long> seg(n,f,g,h,node(0,0),inf);
    vector<node> v(n,node(0,1));
    seg.build(v);

    for (;q--;){
        int c,s,t,x; cin >> c >> s >> t;
        if (!c){
            cin >> x;
            seg.update(s,t+1,x);
        } else cout << seg.query(s,t+1).a << '\n';
    }
}

// https://atcoder.jp/contests/abc177/tasks/abc177_f

void ABC177_F(){
    const int INF=1e9;
    int H,W; cin >> H >> W;
    vector<int> A(H),B(H);
    for (int i=0;i<H;++i) cin >> A[i] >> B[i],--A[i];

    auto f=[](int a,int b){return min(a,b);};
    auto g=[](int a,int x){return x!=-1?x:a;};
    auto h=[](int a,int b){return b;};
    LazySegmentTree<int,int> seg(W,f,g,h,INF,-1)
                            ,nseg(W,f,g,h,W,-1);
    vector<int> v(W,0);
    seg.build(v);
    iota(v.begin(),v.end(),0);
    nseg.build(v);

    for (int i=0;i<H;++i){
        int l=nseg.find_last(W,[&](int x){return x<A[i];})+1;
        int r=nseg.find_last(W,[&](int x){return x<B[i];});
        if (~r&&B[i]<W) seg.set_val(B[i],min(seg[B[i]],B[i]-r));
        nseg.update(l,r+1,B[i]);
        seg.update(A[i],B[i],INF);
        int ans=seg.query(0,W);
        cout << (ans==INF?-1:ans+i+1) << '\n';
    }
}

// https://codeforces.com/contest/1179/problem/C

void CFR569_C(){
    const int MAX_A=1e6+10,INF=1e9;
    int n,m; cin >> n >> m;
    vector<int> a(n),b(m);
    for (int i=0;i<n;++i) cin >> a[i];
    for (int i=0;i<m;++i) cin >> b[i];

    auto f=[](int a,int b){return min(a,b);};
    auto g=[](int a,int b){return a+b;};
    LazySegmentTree<int,int> seg(MAX_A,f,g,g,INF,0);
    vector<int> v(MAX_A,0);
    seg.build(v);
    for (int i=0;i<n;++i) seg.update(a[i],MAX_A,1);
    for (int i=0;i<m;++i) seg.update(b[i],MAX_A,-1);

    int q; cin >> q;
    for (;q--;){
        int t,i,x; cin >> t >> i >> x; --i;
        if (t==1){
            seg.update(a[i],MAX_A,-1);
            a[i]=x;
            seg.update(a[i],MAX_A,1);
        } else {
            seg.update(b[i],MAX_A,1);
            b[i]=x;
            seg.update(b[i],MAX_A,-1);
        }
        int ans=seg.find_last(MAX_A,[&](int x){return x<n-m;});
        cout << (ans<0?-1:ans+1) << '\n';
    }
}

// https://atcoder.jp/contests/practice2/tasks/practice2_k

template<uint_fast64_t Modulus> class modint{
    using u64=uint_fast64_t;
    public:
    u64 a;
    constexpr modint(const u64 x=0) noexcept:a(x%Modulus){}
    constexpr u64 &value() noexcept{return a;}
    constexpr const u64 &value() const noexcept{return a;}
    constexpr modint &operator+=(const modint &rhs) noexcept{
        a+=rhs.a;
        if (a>=Modulus) a-=Modulus;
        return *this;
    }
    constexpr modint operator+(const modint &rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint &operator++() noexcept{
        return ++a,*this;
    }
    constexpr modint operator++(int) noexcept{
        modint t=*this; return ++a,t;
    }
    constexpr modint &operator-=(const modint &rhs) noexcept{
        if (a<rhs.a) a+=Modulus;
        a-=rhs.a;
        return *this;
    }
    constexpr modint operator-(const modint &rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint &operator--() noexcept{
        return --a,*this;
    }
    constexpr modint operator--(int) noexcept{
        modint t=*this; return --a,t;
    }
    constexpr modint &operator*=(const modint &rhs) noexcept{
        a=a*rhs.a%Modulus;
        return *this;
    }
    constexpr modint operator*(const modint &rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        u64 exp=Modulus-2;
        while(exp){
            if (exp&1) *this*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return *this;
    }
    constexpr modint operator/(const modint &rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint operator-() const noexcept{
        return modint(Modulus-a);
    }
    constexpr bool operator==(const modint &rhs) const noexcept{
        return a==rhs.a;
    }
    constexpr bool operator!=(const modint &rhs) const noexcept{
        return a!=rhs.a;
    }
    constexpr bool operator!() const noexcept{return !a;}
    friend constexpr modint pow(modint rhs,long long exp) noexcept{
        modint res{1};
        while(exp){
            if (exp&1) res*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return res;
    }
    template<class T> friend constexpr modint operator+(T x,modint y) noexcept{
        return modint(x)+y;
    }
    template<class T> friend constexpr modint operator-(T x,modint y) noexcept{
        return modint(x)-y;
    }
    template<class T> friend constexpr modint operator*(T x,modint y) noexcept{
        return modint(x)*y;
    }
    template<class T> friend constexpr modint operator/(T x,modint y) noexcept{
        return modint(x)/y;
    }
    friend ostream &operator<<(ostream &s,const modint &rhs) noexcept{
        return s << rhs.a;
    }
    friend istream &operator>>(istream &s,modint &rhs) noexcept{
        u64 a; rhs=modint{(s >> a,a)}; return s;
    }
};

const int MOD=998244353;
using mint=modint<MOD>;

void practice2_k(){
    int N,Q; cin >> N >> Q;
    vector<mint> a(N);
    for (int i=0;i<N;++i) cin >> a[i];

    struct node{
        mint sum; int a;
        node(mint sum,int a):sum(sum),a(a){}
    };
    struct add{
        mint b,c;
        add(mint b,mint c):b(b),c(c){}
        const bool operator==(const add &rhs){
            return b==rhs.b&&c==rhs.c;
        }
    };
    auto f=[](node a,node b){return node(a.sum+b.sum,a.a+b.a);};
    auto g=[](node a,add x){return node(a.sum*x.b+x.c*a.a,a.a);};
    auto h=[](add a,add b){return add(a.b*b.b,a.c*b.b+b.c);};
    LazySegmentTree<node,add> seg(N,f,g,h,node(0,0),add(1,0));
    vector<node> v;
    for (int i=0;i<N;++i) v.emplace_back(a[i],1);
    seg.build(v);

    for (;Q--;){
        int t,l,r; cin >> t >> l >> r;
        if (!t){
            mint b,c; cin >> b >> c;
            seg.update(l,r,add(b,c));
        } else cout << seg.query(l,r).sum << '\n';
    }
}

// https://atcoder.jp/contests/practice2/tasks/practice2_l

void practice2_l(){
    int N,Q; cin >> N >> Q;
    vector<int> A(N);
    for (int i=0;i<N;++i) cin >> A[i];

    struct node{
        int a,b; long long c,d;
        node(int a,int b,long long c,long long d):a(a),b(b),c(c),d(d){}
    };
    auto f=[](node a,node b){return node(a.a+b.a,a.b+b.b,a.c+b.c+(long long)a.a*b.b,a.d+b.d+(long long)a.b*b.a);};
    auto g=[](node a,int x){
        if (!x) return a;
        return node(a.b,a.a,a.d,a.c);
    };
    auto h=[](int a,int b){return a^b;};
    LazySegmentTree<node,int> seg(N,f,g,h,node(0,0,0,0),0);
    vector<node> v;
    for (int i=0;i<N;++i){
        if (A[i]) v.emplace_back(0,1,0,0);
        else v.emplace_back(1,0,0,0);
    }
    seg.build(v);

    for (;Q--;){
        int T,L,R; cin >> T >> L >> R;
        if (T==1) seg.update(--L,R,1);
        else cout << seg.query(--L,R).d << '\n';
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    practice2_l();
}

Measured memory_bytes: 24668