#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
const int MOD = 998244353;
int pre[maxn], inv[maxn], n, m, i, j, k, ans, tot, po[maxn];
inline int ksm(int x, int y, int z) {
  int b = 1;
  while (y) {
    if (y & 1) b = 1LL * b * x % z;
    x = 1LL * x * x % z;
    y >>= 1;
  }
  return b;
}
inline int C(int n, int m) {
  return 1LL * pre[n] * inv[m] % MOD * inv[n - m] % MOD;
}
inline int getans() {
  int ans = 0;
  for (i = 0; i < n; i++) {
    int mul = i + 1;
    if (mul & 1)
      mul = MOD - 1;
    else
      mul = 1;
    mul = 1LL * mul * C(n, i) % MOD;
    tot = (ksm(1 + MOD - po[i], n, MOD) + MOD - ksm(MOD - po[i], n, MOD)) % MOD;
    ans = (ans + 1LL * mul * tot) % MOD;
  }
  return 3LL * ans % MOD;
}
int main() {
  cin >> n;
  pre[0] = po[0] = 1;
  for (i = 1; i <= n; i++) {
    pre[i] = 1LL * pre[i - 1] * i % MOD;
    po[i] = 3LL * po[i - 1] % MOD;
  }
  inv[n] = ksm(pre[n], MOD - 2, MOD);
  for (i = n - 1; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
  for (j = 1; j <= n; j++) {
    int mul = ((j + 1) & 1) ? MOD - 1 : 1;
    ans = (ans + 1LL * mul * C(n, j) % MOD *
                     ksm(3, 1LL * (n - j) * n % (MOD - 1) + j, MOD)) %
          MOD;
  }
  ans = ans * 2 % MOD;
  cout << (ans + getans()) % MOD << endl;
}
