#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const int MAXN = 1000100;
int N;
long long nc[MAXN];
long long cpow(long long b, long long p) {
  if (p == 0) return 1;
  if (p == 1) return b;
  long long v = cpow(b, p / 2);
  v = (v * v) % MOD;
  if (p & 1) v = (v * b) % MOD;
  return v;
}
long long inv(long long b) { return cpow(b % MOD, MOD - 2); }
int main() {
  cin >> N;
  nc[0] = 1;
  for (int i = 1; i <= N; i++) {
    nc[i] = (nc[i - 1] * (N + 1 - i)) % MOD;
    nc[i] = (nc[i] * inv(i)) % MOD;
  }
  long long res = 0;
  for (int i = 1; i <= N; i++) {
    long long x = (3 * nc[i]) % MOD;
    if (i % 2 == 0) x = (MOD - x) % MOD;
    long long p3 = cpow(3, N - i);
    long long np = p3 - 1;
    np = cpow(np, N);
    np += cpow(p3, N) * (2 * cpow(3, i - 1) - 1);
    np = (np + MOD) % MOD;
    x = (x * np) % MOD;
    res = (res + x) % MOD;
  }
  cout << res << "\n";
}
