Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sum of product 2

/*
Sum of product 2:
*/
/*
Python code:
a = [0]*1000001
b = [0]*1000001
d = [0]*1000001
m = 998244353

def f_1(x,y):
    if x<0 or y>x:
        return 0
    return a[x]*d[y]%m*d[x-y]%m

a[0] = b[0] = b[1] = d[0] = d[1] = 1

for i in range(1,1000001):
    a[i] = a[i-1]*i%m

for i in range(2,1000001):
    b[i] = m-m//i*b[m%i]%m

for i in range(2,1000001):
    d[i] = d[i-1]*b[i]%m

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    c = c1 = r = 0
    for i in range(n):
        if arr[i] == 0:
            c += 1
        else:
            c1 += 1
    for i in range(c1+1):
        r = (r+i*f_1(c1+c-i,c))%m
    print((((r*(c+1)-f_1(c1+c-2,c-1))%m+m)%m+f_1(c1+c-2,c-1))*a[c1]%m*a[c]%m)
*/
/* C++ code:
*/
#include<bits/stdc++.h>
#define MOD 998244353
using namespace std;
long long fac[1000010], inv[1000010], finv[1000010];
long long C(long long x, long long y) {
  if (x < 0 || y > x)
    return 0;
  return fac[x] * finv[y] % MOD * finv[x - y] % MOD;
}
#define int long long
void solve() {
  int n,x,c0=0,c1=0,t=0;
  cin >> n;
  for(int i=1; i<=n; ++i) {
    cin >> x;
    c0+=x==0;
    c1+=x==1;
  }
  for(int i=0; i<=c1; ++i)
    t=(t+i*C(c1+c0-i,c0))%MOD;
  cout << (((t*(c0+1)-C(c1+c0-2,c0-1))%MOD+MOD)%MOD+C(c1+c0-2,c0-1))*fac[c1]%MOD*fac[c0]%MOD << endl;
}
signed main() {
  fac[0] = inv[0] = inv[1] = finv[0] = finv[1] = 1;
  for (long long i = 1; i <= 1000000; ++i)
    fac[i] = fac[i - 1] * i % MOD;
  for (long long i = 2; i <= 1000000; ++i)
    inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;
  for (long long i = 2; i <= 1000000; ++i)
    finv[i] = finv[i - 1] * inv[i] % MOD;
  int T;
  cin >> T;
  while(T--) solve();
}
Comment

sum of product 1

# Sum of Product 1:
# First system:

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    res = 0
    c = 0
    for i in range(n):
        if arr[i] == 1:
            c += 1
        else:
            res += c*(c+1)//2
            c = 0
    res += c*(c+1)//2
    print(res)
    
# Second system:

'''
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    res = 0
    c = 0
    for i in range(n):
        if arr[i] == 0:
            c = 0
        else:
            c += 1
        res += c
    print(res)
'''
Comment

sum of product 2

/*
Sum of product 2:
*/
/*
Python code:
a = [0]*1000001
b = [0]*1000001
d = [0]*1000001
m = 998244353

def f_1(x,y):
    if x<0 or y>x:
        return 0
    return a[x]*d[y]%m*d[x-y]%m

a[0] = b[0] = b[1] = d[0] = d[1] = 1

for i in range(1,1000001):
    a[i] = a[i-1]*i%m

for i in range(2,1000001):
    b[i] = m-m//i*b[m%i]%m

for i in range(2,1000001):
    d[i] = d[i-1]*b[i]%m

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    c = c1 = r = 0
    for i in range(n):
        if arr[i] == 0:
            c += 1
        else:
            c1 += 1
    for i in range(c1+1):
        r = (r+i*f_1(c1+c-i,c))%m
    print((((r*(c+1)-f_1(c1+c-2,c-1))%m+m)%m+f_1(c1+c-2,c-1))*a[c1]%m*a[c]%m)
*/
/* C++ code:
*/
#include<bits/stdc++.h>
#define MOD 998244353
using namespace std;
long long fac[1000010], inv[1000010], finv[1000010];
long long C(long long x, long long y) {
  if (x < 0 || y > x)
    return 0;
  return fac[x] * finv[y] % MOD * finv[x - y] % MOD;
}
#define int long long
void solve() {
  int n,x,c0=0,c1=0,t=0;
  cin >> n;
  for(int i=1; i<=n; ++i) {
    cin >> x;
    c0+=x==0;
    c1+=x==1;
  }
  for(int i=0; i<=c1; ++i)
    t=(t+i*C(c1+c0-i,c0))%MOD;
  cout << (((t*(c0+1)-C(c1+c0-2,c0-1))%MOD+MOD)%MOD+C(c1+c0-2,c0-1))*fac[c1]%MOD*fac[c0]%MOD << endl;
}
signed main() {
  fac[0] = inv[0] = inv[1] = finv[0] = finv[1] = 1;
  for (long long i = 1; i <= 1000000; ++i)
    fac[i] = fac[i - 1] * i % MOD;
  for (long long i = 2; i <= 1000000; ++i)
    inv[i] = MOD - MOD / i * inv[MOD % i] % MOD;
  for (long long i = 2; i <= 1000000; ++i)
    finv[i] = finv[i - 1] * inv[i] % MOD;
  int T;
  cin >> T;
  while(T--) solve();
}
Comment

sum of product 1

# Sum of Product 1:
# First system:

for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    res = 0
    c = 0
    for i in range(n):
        if arr[i] == 1:
            c += 1
        else:
            res += c*(c+1)//2
            c = 0
    res += c*(c+1)//2
    print(res)
    
# Second system:

'''
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int,input().split()))[:n]
    res = 0
    c = 0
    for i in range(n):
        if arr[i] == 0:
            c = 0
        else:
            c += 1
        res += c
    print(res)
'''
Comment

PREVIOUS NEXT
Code Example
Python :: SUMOFPROD1 Solution 
Python :: teardown module pytest 
Python :: add a column with initial value to an existing dataframe 
Python :: python3 call parent constructor 
Python :: iterrrows 
Python :: python write into a file 
Python :: python generate dictionary in loop 
Python :: django email 
Python :: how to use ternary operater in python 
Python :: how to use prettytable in python 
Python :: python windows os.listdir path usage 
Python :: python how to check if a dictionary key exists 
Python :: python keyboard input arrow keys 
Python :: max function python 
Python :: abstarct class python 
Python :: python3 format leading 0 
Python :: numpy round to nearest 5 
Python :: python print every character in list as string 
Python :: heapsort python 
Python :: Insert list element at specific index 
Python :: To Divide or Not To Divide codechef solution 
Python :: python logging variables extra 
Python :: onedrive python upload 
Python :: how to extract dictionary value from string in python 
Python :: writing to a file, with echo 
Python :: creating numpy array using empty 
Python :: python program to reverse a list 
Python :: pandas disply options 
Python :: Install pygmt in Anaconda prompt 
Python :: python loop with index 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =