#include <bits/stdc++.h>
long long n, ans, md = 998244353, num[1000010], inv[1000010];
long long power(long long x, long long p) {
  long long num = 1;
  while (p) {
    if (p & 1) num = num * x % md;
    x = x * x % md;
    p >>= 1;
  }
  return num;
}
int main() {
  scanf("%I64d", &n);
  num[0] = 1;
  for (int i = 1; i <= n; ++i) num[i] = (num[i - 1] * i) % md;
  for (int i = 0; i <= n; ++i) inv[i] = power(num[i], md - 2);
  for (int i = 1; i <= n; ++i)
    ans =
        (ans + power(md - 1, i + 1) * (num[n] * inv[i] % md * inv[n - i] % md) %
                   md * power(3, i) % md * power(3, n * (n - i)) % md) %
        md;
  ans = (ans << 1) % md;
  for (int i = 0; i < n; ++i) {
    long long x = (power((1 + md - power(3, i)) % md, n) + md -
                   power(md - power(3, i), n)) %
                  md;
    ans = (ans + 3 * power(md - 1, i + 1) % md *
                     (num[n] * inv[i] % md * inv[n - i] % md) % md * x % md) %
          md;
  }
  printf("%I64d\n", ans);
  return 0;
}
