Multiply Valid Integers (Mod)
Python
Medium
4 views
Problem Description
Read n tokens. Multiply only valid integers and take modulo 1000000007. If no valid integers, output 0.
Input Format
First line n. Second line n tokens.
Output Format
One integer result.
Official Solution
import sys
MOD=1000000007
lines=sys.stdin.read().splitlines()
if len(lines)<2:
sys.stdout.write('0')
else:
try:
n=int(lines[0].strip())
except Exception:
sys.stdout.write('0')
raise SystemExit
toks=lines[1].split()[:n]
ok=False
res=1
for t in toks:
try:
v=int(t)
res=(res*(v%MOD))%MOD
ok=True
except Exception:
pass
sys.stdout.write(str(res if ok else 0))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!